Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simple searches including metatags result in timeouts #672

Closed
ToksT opened this issue Feb 27, 2013 · 2 comments
Closed

Some simple searches including metatags result in timeouts #672

ToksT opened this issue Feb 27, 2013 · 2 comments

Comments

@ToksT
Copy link
Contributor

ToksT commented Feb 27, 2013

"gentags:1 -tagme": http://danbooru.donmai.us/posts?tags=gentags:1+-tagme
"date:2006-01-01..2006-05-01": http://danbooru.donmai.us/posts?tags=date:2006-01-01..2006-05-01
Both result in timeouts, even though they shouldn't even be that large.

For date searches, some work and some don't. This works, even though it should be larger than the one above:
"date:2006-01-01..2012-01-01": http://danbooru.donmai.us/posts?tags=date:2006-01-01..2012-01-01

@ToksT
Copy link
Contributor Author

ToksT commented Dec 16, 2013

I'm still not entirely sure what the cause is, but I noticed something for those date searches: If you changed the ordering to posts.created_at DESC instead of the normal posts.id DESC, then they'll be much faster.

Maybe changing the default order to created at if and only if the date: metatag is present would be an idea.

@evazion
Copy link
Member

evazion commented May 23, 2017

gentags:1 -tagme is slow because tag_count_general isn't indexed, -tagme can't be indexed, so the query degrades to a full table scan. tag_count_general isn't worth indexing IMO, so it can't be helped.

date:2006-01-01..2006-05-01 is slow because instead of filtering by the date, it does a downwards index scan on posts.id, which leads to scanning ~2.5 million posts until it reaches 2006:

EXPLAIN (analyze) SELECT "posts".* FROM "posts" WHERE (true) AND (posts.created_at BETWEEN '2006-01-01 05:00:00.000000' AND '2006-05-01 04:00:00.000000') AND (true /* PostSets::Post#posts:2 */) ORDER BY posts.id DESC LIMIT 20 OFFSET 0;

 Limit  (cost=0.43..1055.76 rows=20 width=1171) (actual time=2158.938..2158.962 rows=20 loops=1)
   ->  Index Scan Backward using posts_pkey on posts  (cost=0.43..881832.15 rows=16712 width=1171) (actual time=2158.935..2158.958 rows=20 loops=1)
         Filter: ((created_at >= '2006-01-01 05:00:00'::timestamp without time zone) AND (created_at <= '2006-05-01 04:00:00'::timestamp without time zone))
         Rows Removed by Filter: 2540515
 Planning time: 0.367 ms
 Execution time: 2159.024 ms

Ordering by posts.created_at DESC allows it to use the created_at index to both filter and order by date:

EXPLAIN (analyze) SELECT "posts".* FROM "posts" WHERE (true) AND (posts.created_at BETWEEN '2006-01-01 05:00:00.000000' AND '2006-05-01 04:00:00.000000') AND (true /* PostSets::Post#posts:2 */) ORDER BY posts.created_at DESC LIMIT 20 OFFSET 0;

 Limit  (cost=0.43..28.07 rows=20 width=1171) (actual time=0.146..0.166 rows=20 loops=1)
   ->  Index Scan Backward using index_posts_on_created_at on posts  (cost=0.43..23096.43 rows=16712 width=1171) (actual time=0.145..0.162 rows=20 loops=1)
         Index Cond: ((created_at >= '2006-01-01 05:00:00'::timestamp without time zone) AND (created_at <= '2006-05-01 04:00:00'::timestamp without time zone))
 Planning time: 0.371 ms
 Execution time: 0.253 ms

evazion added a commit to evazion/danbooru that referenced this issue May 23, 2017
r888888888 added a commit that referenced this issue May 23, 2017
Fix date:, age: metatags to use the index. (fix #672).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants