|
Posted by Toby A Inkster on 06/08/07 10:26
Geoff Berrow wrote:
> $sql="SELECT DISTINCT category FROM table WHERE category!=''";
Although tempting to use, SELECT DISTINCT is normally a sign of a place
where things could be done better.
SELECT MIN(category) FROM table WHERE category!='' GROUP BY category;
will achieve the same effect, but on most databases will run slightly
faster. This is because SELECT DISTINCT effectively fetches rows and then
discards them, whereas GROUP BY only fetches the needed rows to begin with.
If you don't believe me, try using the "EXPLAIN" feature on your database
to see which is faster.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 104 days, 18:07.]
URLs in demiblog
http://tobyinkster.co.uk/blog/2007/05/31/demiblog-urls/
Navigation:
[Reply to this message]
|