Active Topics Active Topics  Display List of Forum Members Memberlist  Invert Forum Background Colours Invert Colours  Search The Forum Search  Help Help  Register Register  Login Login
Sponsored by Beatnik Games
Hardware / Software Bits & Bobs
 |SFH| Sent From Hell :: Community Forums : Hardware / Software Bits & Bobs
Subject Topic: I’m SQL Happy :) (Topic Closed Topic Closed) Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
Nhumrod
Site Admin
Site Admin
Avatar
Clan Leader & GM

Joined: 09 September 2002
Location: Scotland
Posts: 13897
Posted: 26 October 2006 at 3:50pm | IP Logged  

WARNING - FOR SQL GEEKS ONLY!

I managed to reqrite a SQL query I wrote earlier this week such that it now takes less than a second to run, rather than a minute and a half! Geeky I know... but I was well chuffed!

Original query:
SELECT DISTINCT tblServerHotfixes.name, tblServerHotfixes.hotfix_name
FROM ((tblServerHotfixes INNER JOIN tblServers ON tblServerHotfixes.name=tblServers.name) INNER JOIN relnHotfixToGroup ON tblServers.hotfix_group_id=relnHotfixToGroup.hotfix_group_id) INNER JOIN tblHotfix ON relnHotfixToGroup.hotfix_id=tblHotfix.id
WHERE (((Exists (select * from qryListReqdHotfixes WHERE tblServerHotfixes.name = qryListReqdHotfixes.name AND tblServerHotfixes.hotfix_name = qryListReqdHotfixes.hotfix_name))=False))
ORDER BY tblServerHotfixes.name, tblServerHotfixes.hotfix_name;

Is now:
SELECT H1.name, H1.hotfix_name AS extra_hotfix
FROM tblServerHotfixes AS H1
WHERE H1.name IN (SELECT name FROM tblServers WHERE tblServers.hotfix_group_id > 0)
AND H1.hotfix_name NOT IN (
SELECT tblHotfix.hotfix_name
FROM tblServers, relnHotfixToGroup, tblHotfix
WHERE tblServers.name = H1.name
AND tblServers.hotfix_group_id=relnHotfixToGroup.hotfix_group_id
AND relnHotfixToGroup.hotfix_id=tblHotfix.id)
ORDER BY H1.name, H1.hotfix_name;

I think this proves that joining tables using SELECT is FAR FAR FAR better than joining them with INNER JOINs... (also - never use SELECT DISTINCT if you can help it... if you have to use DISTINCT, you're probably SELECTing the wrong data anyway!) 

Another tip - using EXISTS is better than "IN" if youre doing positive searches as EXISTS will exit the search when it finds the 1st object, but "IN" will be forced to search the entire table...



Edited by Demonboy on 26 October 2006 at 3:51pm


__________________
There is no IRL, there's just AFK.
Back to Top View Nhumrod's Profile Search for other posts by Nhumrod Visit Nhumrod's Homepage Send Private Message Add to Buddy List
 
Mash-Tin_UK
Clan Member
Clan Member
Avatar

Joined: 21 September 2005
Location: United Kingdom
Posts: 1936
Posted: 26 October 2006 at 5:13pm | IP Logged  

 lol

__________________

Your tears fuel me     -      E .� ` ' / �. F
Back to Top View Mash-Tin_UK's Profile Search for other posts by Mash-Tin_UK Send Private Message Add to Buddy List
 
Puss
Honourary Member
Honourary Member
Avatar

Joined: 04 August 2003
Location: Scotland
Posts: 1518
Posted: 26 October 2006 at 7:20pm | IP Logged  

Handy tips. Will keep them in mind when I do some more SQL stuff.

__________________
You're about as useful as a cock flavored lollipop.
Back to Top View Puss's Profile Search for other posts by Puss Send Private Message Add to Buddy List
 
Sirwomble
Site Admin
Site Admin
Avatar
Occasional Table

Joined: 09 September 2002
Location: United Kingdom
Posts: 6687
Posted: 26 October 2006 at 11:02pm | IP Logged  



__________________

Back to Top View Sirwomble's Profile Search for other posts by Sirwomble Send Private Message Add to Buddy List
 
TheGouldFish
Honourary Member
Honourary Member
Avatar
The Mapper

Joined: 05 June 2003
Location: Scotland
Posts: 3456
Posted: 27 October 2006 at 10:39am | IP Logged  

Not sure I would agree on the inner join, and there are a number of places where distinct is usefull.

but the rest sounds good.

__________________
TheGouldFish
=============
Fear the Fish
Back to Top View TheGouldFish's Profile Search for other posts by TheGouldFish Visit TheGouldFish's Homepage Send Private Message Add to Buddy List
 

Sorry, you can NOT post a reply.
This topic is closed.

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 1.7813 seconds.

Sponsored by Beatnik Games