LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-28-2017, 04:31 PM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Slow MySQL query


I created an innocent looking MySQL query which eventually took up to 10 minutes to complete. I am posting this here so you don't have to make the same stupid mistake as I did.

Code:
SELECT report_dt, ps, SUBTIME(report_dt, "04:00") AS local_dt FROM \
pms_meas_log WHERE SUBTIME(report_dt, "04:00") < "2016-11-26 23:59" AND \
dev = "h19_pm" ORDER BY report_dt LIMIT 1;
The table is a simple linear table. report_dt is DATETIME. The table contains 9.2 x 10e6 records. report_dt is the primary index.

All times are recorded in UTC. But the client is in UTC-4. So in the select statement local_dt is an alias for SUBTIME(report_dt, "04:00"). So UTC-4.

The problem is in this part:
Code:
WHERE SUBTIME(report_dt, "04:00") < "2016-11-26"
One should not have such a function call (or any calculation for that matter) in the WHERE part of a query. MySQL will calculate this value for all 9.2 million records.

This query was created by a model in a PHP framework. The model constructed the query faithfully and as efficient as could be expected. I only should not have used that alias in the WHERE clause. Once I used the plain report_dt in the WHERE clause the query run in less than 7 seconds.

jlinkels

Last edited by jlinkels; 06-28-2017 at 04:35 PM.
 
Old 06-28-2017, 07:52 PM   #2
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Yeah, running a function on your indexed column makes the index useless and you get a full table scan.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using mysqldumpslow to read the mysql slow query log LXer Syndicated Linux News 0 05-02-2017 02:24 PM
Slow mysql query, indexing doesn't help. Suggestions? sneakyimp Linux - Server 3 10-08-2009 08:00 PM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 07:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration