Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum length of an SQL Query

SELECT f.* 
FROM feeds f, 
     user_feeds uf 
WHERE (f.id=uf.feed_id and uf.user_id in (1,2,5,6,23,45)) 
ORDER BY created_at DESC

This is a query used to construct a user's feeds. The issue I have with this query is that the "uf.user_id in ()" increases as the number of users the user follows increases.

What is the allowed max length of an SQL query? Is there a better way to implement the above query?

Note: I am using ActiveRecord and Postgres.

like image 353
Prakash Raman Avatar asked Feb 08 '11 18:02

Prakash Raman


People also ask

Is there a character limit in SQL query?

Microsoft SQL Server 2008 (and above) can store up to 8000 characters as the maximum length of the string using varchar data type. SQL varchar usually holds 1 byte per character and 2 more bytes for the length information.

How do you find max and min length in SQL?

SQL Server databases use LEN or DATALENGTH to find field width. It also has its own function to find the maximum length of a column – COL_LENGTH. SELECT MIN(LENGTH(<column_name>)) AS MinColumnLength FROM Table; If we include any non-aggregate functions into our query then we need a GROUP BY clause.

How long can queries be?

Although officially there is no limit specified by RFC 2616, many security protocols and recommendations state that maxQueryStrings on a server should be set to a maximum character limit of 1024. While the entire URL, including the querystring, should be set to a max of 2048 characters.


3 Answers

The maximum length of a query that PostgreSQL can process is 2147483648 characters (signed 4-byte integer; see src/include/lib/stringinfo.h).

like image 109
Peter Eisentraut Avatar answered Oct 12 '22 06:10

Peter Eisentraut


To avoid the query size, you could replace the IN (1, 2) with IN (select followed_id from following where follower_id = ?) or whatever the appropriate query would be to find the ids of the followed users from the follower's id.

like image 20
William Avatar answered Oct 12 '22 07:10

William


While there is no (real) limit on the length of the query string, there is a limit on the number of IN (x,y,z...) clauses: 10000, configurable in the postgres.ini-file:

See: http://grokbase.com/t/postgresql/pgsql-general/061mc7pxg6/what-is-the-maximum-length-of-an-in-a-b-c-d-list-in-postgresql :

"In 7.4 and earlier it depends on the max_expr_depth setting." ... "In 8.0 and later max_expr_depth is gone and the limit depends on max_stack_depth."

like image 20
Christian Avatar answered Oct 12 '22 05:10

Christian