Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QUERY speed with limit and milion records

Tags:

sql

php

mysql

Hi i have a 7milion records db table for testing query speed.

I tested up my 2 queries which are the same query with different limit parametres:

query 1 -

SELECT    * 
FROM      table 
LIMIT     20, 50;

query 2 -

SELECT    * 
FROM      table 
LIMIT     6000000, 6000030;

query exec times are:

  1. query 1 - 0.006 sec
  2. query 2 - 5.500 sec

In both of these queries, I am fetching same number of records, but in the second case it's taking more time. Can someone please explain the reasons behind this?

like image 502
itsme Avatar asked Aug 18 '11 03:08

itsme


People also ask

What is fastest way to execute the query with millions of records?

1:- Check Indexes. 2:- There should be indexes on all fields used in the WHERE and JOIN portions of the SQL statement 3:- Limit Size of Your Working Data Set. 4:- Only Select Fields You select as Need. 5:- Remove Unnecessary Table and index 6:- Remove OUTER JOINS.

Can SQL handle 1 million records?

Millions of rows is not a problem, this is what SQL databases are designed to handle, if you have a well designed schema and good indexes.

Does limit make query faster?

The answer, in short, is yes. If you limit your result to 1, then even if you are "expecting" one result, the query will be faster because your database wont look through all your records. It will simply stop once it finds a record that matches your query.

What is the fastest way to search for millions of records in SQL Server?

Domains(DomainName); When you load new data, check if any of the domain names are new - and insert those into the Domains table. Then in your big table, you just include the DomainID. Not only will this keep your 50 million row table much smaller, it will also make lookups like this much more efficient.

How does the speed of the query depend on the number?

The speed of the query depends on the number of rows but if you do appropriate optimizations taking the performance factors such as: the query will execute faster. Show activity on this post.

How many records are there in a 5 minute query?

For query you have to order index fields in range from producing the smallest result set to the broader condition. If your table contains 10 million records for an hour, then 5 min part contain about 1/12 or 800k records. If you have 1000 unique keys f.e. then you have about 1/1000 or 100k records with the same unique key.

Is there a limit on number of rows in a query?

11-17-2019 11:24 PM Limit of one million rows returned on any query: There is a fixed limit of one million rows placed on the number of rows that can be returned in any single query to the underlying source. This generally has no practical implications, and visuals themselves aren’t going to display that many points.

Why are my queries with limits and offsets so slow?

Queries with LIMITs and OFFSETs are common in application that require pagination and in some cases might work well for a while. In many cases though, they become slow and painful once the OFFSET has a high value. Well, in most cases, low offset queries are not slow. The problem starts with high OFFSET values.


3 Answers

Without looking into it too closely, my assumption is that this occurs because the first query only has to read to the 50th record to return results, whereas the second query has to read six million before returning results. Basically, the first query just shorts out quicker.

I would assume that this has an incredible amount to do with the makeup of the table - field types and keys, etc.

If a record is made up of fixed-length fields (e.g. CHAR vs. VARCHAR), then the DBMS can just calculate where the nth record starts and jumps there. If its variable length, then you would have to read the records to determine where the nth record starts. Similarly, I'd further assume that tables which have appropriate primary keys would be quicker to query than those without such keys.

like image 159
AgentConundrum Avatar answered Oct 17 '22 23:10

AgentConundrum


I think the slowdown is tied to the fact you are using limits with offsets and are querying the table with no additional context for indexing. Its possible the first is just faster because it can get to the offset quicker.

like image 6
Jake Dempsey Avatar answered Oct 18 '22 00:10

Jake Dempsey


It's the difference between returning 50 rows and 6000030 rows (or ~1million rows since you said there were only 7million rows).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

http://dev.mysql.com/doc/refman/5.0/en/select.html

Also, I think you're looking for 30 row pages so your queries should be using 30 as the second parameter in the limit clause.

SELECT    * 
FROM      table 
LIMIT     20, 30;

SELECT    * 
FROM      table 
LIMIT     6000000, 30;
like image 4
dotjoe Avatar answered Oct 18 '22 00:10

dotjoe