Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize SELECT query with ORDER BY, OFFSET and LIMIT of postgresql
    primarykey
    data
    text
    <p>This is my table schema</p> <pre><code>Column | Type | Modifiers -------------+------------------------+------------------------------------------------------ id | integer | not null default nextval('message_id_seq'::regclass) date_created | bigint | content | text | user_name | character varying(128) | user_id | character varying(128) | user_type | character varying(8) | user_ip | character varying(128) | user_avatar | character varying(128) | chatbox_id | integer | not null Indexes: "message_pkey" PRIMARY KEY, btree (id) "idx_message_chatbox_id" btree (chatbox_id) "indx_date_created" btree (date_created) Foreign-key constraints: "message_chatbox_id_fkey" FOREIGN KEY (chatbox_id) REFERENCES chatboxes(id) ON UPDATE CASCADE ON DELETE CASCADE </code></pre> <p>This is the query</p> <pre><code>SELECT * FROM message WHERE chatbox_id=$1 ORDER BY date_created OFFSET 0 LIMIT 20; </code></pre> <p>($1 will be replaced by the actual ID)</p> <p>It runs pretty well, but when it reaches 3.7 millions records, all SELECT queries start consuming a lot of CPU and RAM and then the whole system goes down. I have to temporarily backup all the current messages and truncate that table. I am not sure what is going on because everything is ok when I have about 2 millions records</p> <p>I am using Postresql Server 9.1.5 with default options. </p> <hr> <p>Update the output of EXPLAIN ANALYZE</p> <pre><code>Limit (cost=0.00..6.50 rows=20 width=99) (actual time=0.107..0.295 rows=20 loops=1) -&gt; Index Scan Backward using indx_date_created on message (cost=0.00..3458.77 rows=10646 width=99) (actual time=0.105..0.287 rows=20 loops=1) Filter: (chatbox_id = 25065) Total runtime: 0.376 ms (4 rows) </code></pre> <hr> <p>Update server specification</p> <pre><code>Intel Xeon 5620 8x2.40GHz+HT 12GB DDR3 1333 ECC SSD Intel X25-E Extreme 64GB </code></pre> <hr> <p><strong>Final solution</strong></p> <p>Finally I can go above 3 million messages, I have to optimize the postgresql configuration as wildplasser suggested and also make a new index as A.H. suggested</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload