Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: How to count rows using DBI & mysql - Performance results

by Brovnik (Hermit)
on Jun 11, 2001 at 15:37 UTC ( #87449=note: print w/ replies, xml ) Need Help??


in reply to Re: How to count rows using DBI & mysql - Performance results
in thread How to count rows using DBI & mysql - Performance results

Useful comment. I hadn't thought of getting SQL to do the logic.

An index on the columns in where clause might have a huge impact if you use count(*).

I believe that MySQL creates an index on the leftmost subset of columns anyway, due to the way it stores the data. It should be intelligent enough to only compare the second row if the first row succeeds. In the test case, only there are only 4 rows, so testing the remainder of the AND should be quick.

Have just tested the suggestion. In fact, not having indexes doesn't seem to make any difference (1230/sec vs. 1174/sec), so I think I am correct that there is an index by default on the leftmost column.
--
Brovnik


Comment on Re: Re: How to count rows using DBI & mysql - Performance results
Re: Re: Re: How to count rows using DBI & mysql - Performance results
by busunsl (Vicar) on Jun 11, 2001 at 18:09 UTC
    Most RDBMS won't even consider using an index on a table with only 4 rows due to the overhead in I/O and processing.

    I created the table in a Sybase database, filled it with about 2500 rows with my db-stress tool and tested against it (with only 1000 iterations).

      Good point. I'm not sure how intelligent MySQL is, but the optimizer in Oracle will make this choice.

      If you submit a count(*) query on a table with a primary key, it will use the index to generate the count instead of actually counting all the rows in the table.

      The same holds true when doing a count(colname) and an index exists on that column.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://87449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-09-22 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls