Ano-Tech Computers
Enter keyword:

Mysql: Optimize OR clauses using UNION
Problem:
On large tables, a query such as the following one will optimize poorly and use the index for the OR'ed column inefficiently: SELECT * FROM syslog WHERE ( prog = 'sshd' OR prog = 'sshd(pam_unix)' ) AND user = 'floyd' ORDER BY timestamp DESC LIMIT 10 The result is surprisingly poor performance, even if both the "prog" and "user" columns are properly indexed.
 
Solution:
Rewrite using UNION:

(
SELECT * FROM syslog
WHERE prog = 'sshd'
AND user = 'floyd'
) UNION (
SELECT * FROM syslog
WHERE prog = 'sshd(pam_unix)'
AND user = 'floyd'
)
ORDER BY timestamp DESC
LIMIT 10

The resulting dataset is exactly the same, but now you're telling MySQL explicitly to look for two distinct combinations of data. This lets MySQL use the index for "prog" more efficiently and the result is a significant performance boost.

Note that UNION was implemented in MySQL 4.x
 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles