Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get rows_examined in MySQL without the slow log?

I'm building some profile information for a home grown app. I'd like the debug page to show the query sent along with how many rows were examined without assuming that slow_log is turned on, let alone parsing it.

Back in 2006, what I wanted was not possible. Is that still true today?

I see Peter Zaitsev has a technique where you:

  1. Run FLUSH STATUS;
  2. Run the query.
  3. Run SHOW STATUS LIKE "Handler%";

and then in the output:

Handler_read_next=42250 means 42250 rows were analyzed during this scan

which sounds like if MySQL is only examining indexes, it should give you the number. But are there a set of status vars you can poll, add up and find out how many rows examined? Any other ideas?

like image 858
joedevon Avatar asked Oct 28 '09 14:10

joedevon


People also ask

How do I disable slow query log in MySQL?

To disable or enable the slow query log or change the log file name at runtime, use the global slow_query_log and slow_query_log_file system variables. Set slow_query_log to 0 to disable the log or to 1 to enable it.

Can I delete MySQL slow log?

You cannot delete the file, when mysql service is accessing the log file.

How do I find the slow query log path in MySQL?

By default, the slow query log file is located at /var/lib/mysql/hostname-slow. log.


2 Answers

It's slightly better than it was in 2006. You can issue SHOW SESSION STATUS before and after and then look at each of the Handler_read_* counts in order to be able to tell the number of rows examined.

There's really no other way.. While the server protocol has a flag to say if a table scan occurred, it doesn't expose rows_examined. Even tools like MySQL's Query Analyzer have to work by running SHOW SESSION STATUS before/after (although I think it only runs SHOW SESSION STATUS after, since it remembers the previous values).

I know it's not related to your original question, but there are other expensive components to queries besides rows_examined. If you choose to do this via the slow log, you should check out this patch:

http://www.percona.com/docs/wiki/patches:microslow_innodb#changes_to_the_log_format

I can recommend looking for "Disk_tmp_table: Yes" and "Disk_filesort: Yes".

like image 178
Morgan Tocker Avatar answered Nov 15 '22 10:11

Morgan Tocker


Starting in 5.6.3, the MySQL performance_schema database also exposes statements statistics, in tables such as performance_schema.events_statements_current.

The statistics collected by statements include the 'ROWS_EXAMINED' column.

See http://dev.mysql.com/doc/refman/5.6/en/events-statements-current-table.html

From there, statistics are aggregated to provide summaries.

See http://dev.mysql.com/doc/refman/5.6/en/statement-summary-tables.html

like image 39
Marc Alff Avatar answered Nov 15 '22 09:11

Marc Alff