lundi 22 décembre 2014

Pagination - Caching


I am working on pagination for a complex join query and unfortunately I am not sure what's the best way to go... Right now I am thinking about the following solution.


Imagine that SELECT id... is the complex query. I copy all id's to the temporary cache.



CREATE TEMPORARY TABLE tmp SELECT id FROM comments WHERE user_id = 123;


Now I will check number of selected records.



SELECT COUNT(*) FROM tmp;


If a number of records is let's say greater than 1000 all records will be moved to a cache table and then selected by 100's (All of them - So it is not a classic pagination, all records are going to be selected ,but in smaller parts). However if number of records is < 1000 this query will be executed



SELECT column_names FROM comments c JOIN tmp t ON c.id = t.id;


or if I select a data from cache



SELECT column_names FROM comments c JOIN cache_table ct ON c.id = ct.id LIMIT X;


So basically I can select all records directly instead of joining multiple tables over and over...


My problem with this solution is probably the fact that I have to created a temporary table (every client has to do it), luckily only for very short period of time. Is there some another way how to do this? What I would like to achieve is something like:



resultSet = SELECT id FROM comments WHERE user_id = 123;

if (resultSet.length < 1000)
just select all data (Use already selected id's for select)
else
copy ID's to cache


So I don't want to run the complex query many times also I don't want to use the cache if it is not really required.





Aucun commentaire:

Enregistrer un commentaire