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.
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.
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.
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.
The maximum length of a query that PostgreSQL can process is 2147483648 characters (signed 4-byte integer; see src/include/lib/stringinfo.h
).
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.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With