NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP array_unique vs. SQL DISTINCT

Adrian Noland anoland at indigente.net
Sun Jun 3 18:07:54 EDT 2007


On 6/3/07, David Krings <ramons at gmx.net> wrote:
>
> David Krings wrote:
> > Hi!
> >
> > Which one is faster: PHP array_unique or SQL DISTINCT ?
> >
>
> OK, while I still like to know what the answer is, the DISTINCT wasn't
> the culprit, but the way I join the tables. I ran the query across only
> two tables and with that got millions of records when I expected only a
> few hundred.
> I guess it is time to learn some more SQL and not just go for broke and
> run queries without knowing what they really do. Worked fine in the test
> system with only a dozen records in each table. My approach apparently
> doesn't scale well.
>
> David


The key to speedy SQL is in the indexes. Without an index the SQL engine
iterates over every row. With an index it automagically grabs the correct
value.

Try using EXPLAIN to check your queries if they don't seem to be working as
expected.

EXPLAIN SELECT * FROM table_name;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070603/9f309021/attachment.html>


More information about the talk mailing list