Why would adding LIMIT slow down a SQL query?

Questions and answers regarding general SQL and backend databases

Why would adding LIMIT slow down a SQL query?

Postby grahamg » Fri Feb 14, 2014 2:19 pm

Could any of the SQL gurus please educate me on why limiting a SQL query to the last 100 records would cause an unacceptable slowdown in the Query results?

Documents table has about 400k records with 'iddc' being the Integer PK. The code below limits to 100 records - an identical Query without the limit (vServer, query, args, -1) produces much faster results.

Code: Select all
      var query       = "SELECT iddc FROM documents WHERE email_account = ? AND doc_type = ? ORDER BY doc_date desc";
      
      var args       = new Array()
      args[0]       = vEMaccount;      
      args[1]         = vDocType;
      
      var dataset    = databaseManager.getDataSetByQuery(vServer, query, args, 100);
Attachments
Screen Shot 2014-02-14 at 12.06.12.png
Screen Shot 2014-02-14 at 12.06.12.png (35.43 KiB) Viewed 4532 times
Graham Greensall
Worxinfo Ltd
www.worxinfo.com
grahamg
 
Posts: 752
Joined: Fri Oct 03, 2003 3:15 pm
Location: Midlands UK

Re: Why would adding LIMIT slow down a SQL query?

Postby lwjwillemsen » Fri Feb 14, 2014 4:30 pm

Hi Graham,

I guess you don't have an index on the column doc_date ?
In that case the database process to obtain the sorted first 100 records could take some time...

Regards,
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

Re: Why would adding LIMIT slow down a SQL query?

Postby grahamg » Fri Feb 14, 2014 5:29 pm

Thanks Lambert

I had to double-check but yes there is an index on doc_date.

CREATE INDEX doc_date
ON documents
USING btree
(doc_date DESC NULLS LAST);
Graham Greensall
Worxinfo Ltd
www.worxinfo.com
grahamg
 
Posts: 752
Joined: Fri Oct 03, 2003 3:15 pm
Location: Midlands UK

Re: Why would adding LIMIT slow down a SQL query?

Postby lwjwillemsen » Fri Feb 14, 2014 6:51 pm

Hmm, strange...

Have you called this query almost 2000 times with the same parameters ?
In other words : what happens if you put indexes on columns email_account and doc_type ?
Or : this query would perform better if you could use doc_date (date range) in the where clause since
doc_date has an index.

On large tables we always put an index on some kind of date column and tell our customers to use
that column in end-user queries. Our initial form foundset sort is also set to that date column.
The servoy load foundset always works with limit ? and we see great performance gain when filtering and sorting on indexed columns.

Regards,
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

Re: Why would adding LIMIT slow down a SQL query?

Postby grahamg » Fri Feb 14, 2014 7:23 pm

Yes doc_type and email_account also have indexes.

Both Queries are triggered by clicking on tab_labels and will have slightly different parameters each time. The fast - unlimited - query is when Users select their Email_Account + Inbox/Pending/Drafts etc., that will almost always have less than Servoy's 200 record initial load.

The slow - limit=100 - query is for the Emails Sent/Received selections that have many thousands of records - however most of the time they initially only want to see the last day or two. I (mistakenly) thought that by reducing the limit it would be faster.

On next update will remove the LIMIT=100 but was curious to learn what I might be doing wrong.

Appreciate your time on this.
Graham Greensall
Worxinfo Ltd
www.worxinfo.com
grahamg
 
Posts: 752
Joined: Fri Oct 03, 2003 3:15 pm
Location: Midlands UK

Re: Why would adding LIMIT slow down a SQL query?

Postby ROCLASI » Fri Feb 14, 2014 7:45 pm

Hi Graham,

You say the one without the limit gives you already a small resultset (less than 200) and the one with the limit would normally return more than that.
If so then it is not necessarily strange that the query is slower. It will have to select all the records matching your WHERE clause, then SORT it all before it can apply the LIMIT.
How large is your resultset (before the limit) ?
Also doing an EXPLAIN ANALYZE <query> in a query editor will give more insight in how the 2 queries behave (I assume you are using PostgreSQL here).
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Why would adding LIMIT slow down a SQL query?

Postby grahamg » Fri Feb 14, 2014 9:01 pm

Hi Robert

The original Query (without limit) for Inbox/Pending/Drafts will generally just result in 0-100 records.

Selecting all Sent/Received Emails for a persons Email_Account would result in many thousands of records - but generally they only want to check on Sent/Received in last day or so. That gave me the idea of limiting to the last 100 as there is a comprehensive Search tab for checking further.

Yes - I'm using Postgres and will take a look at the Query Analyzer - thanks for the tip.

Have a good weekend.

Graham
Graham Greensall
Worxinfo Ltd
www.worxinfo.com
grahamg
 
Posts: 752
Joined: Fri Oct 03, 2003 3:15 pm
Location: Midlands UK


Return to SQL Databases

Who is online

Users browsing this forum: Bing [Bot] and 8 guests