Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by busunsl (Vicar)
on Jun 11, 2001 at 14:59 UTC ( [id://87444]=note: print w/replies, xml ) Need Help??


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

Some things that might have an influence on the benchmarks, depending on the RDBMS:

If you just test for existence, you can use the exists function in a bit of sql-code:

if not exists (select * from depend where usedby='Depend::Module' AND +uses='DBI') begin insert .... end

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

Replies are listed 'Best First'.
Re: Re: How to count rows using DBI & mysql - Performance results
by Brovnik (Hermit) on Jun 11, 2001 at 15:37 UTC
    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

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://87444]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found