in reply to DBI: Better way to find number of rows in a table?
Not only is it not a Perl question, or even a SQL question, it's a platform-specific MySQL question. In other words, the answer is different for Oracle, DB2, SQL Server, etc. All of these have different catalogs, so the "improved" query would be different. Even knowing the catalog structure won't always help because most databases don't allow variables in the FROM clause: you end up running the same query for each table anyway.
There is a better way to ask if a table has rows, but I'm not sure if MySQL supports it: the EXISTS clause. In Sybase, for example, you could say:
Otherwise, it's kind of a waste to count all the rows just to see if the table's empty.if exists (select 1 from my_table) print 'my_table has at least one row'
|
|---|