我在我的数据库上遇到查询时间很慢(到目前为止所有的测试都在本地测试过),不知道该怎么做。数据库本身有44个表,其中一些表有超过100万条记录(主要是电影,女演员和演员表)。在PostgreSQL中提高查询速度
该表是通过JMDB使用IMDB上的平面文件创建的。此外,我将要展示的SQL查询来自于所述程序(它也经历了非常慢的搜索时间)。我曾尝试包括尽可能多的信息,我可以,比如查询计划等
"QUERY PLAN"<br />
"HashAggregate (cost=46492.52..46493.50 rows=98 width=46)"<br />
" Output: public.movies.title, public.movies.movieid, public.movies.year"<br />
" -> Append (cost=39094.17..46491.79 rows=98 width=46)"<br />
" -> HashAggregate (cost=39094.17..39094.87 rows=70 width=46)"<br />
" Output: public.movies.title, public.movies.movieid, public.movies.year"<br />
" -> Seq Scan on movies (cost=0.00..39093.65 rows=70 width=46)"<br />
" Output: public.movies.title, public.movies.movieid, public.movies.year"<br />
" Filter: (((title)::text ~~* '%Babe%'::text) AND ((title)::text !~~* '""%}'::text))"<br />
" -> Nested Loop (cost=0.00..7395.94 rows=28 width=46)"<br />
" Output: public.movies.title, public.movies.movieid, public.movies.year"<br />
" -> Seq Scan on akatitles (cost=0.00..7159.24 rows=28 width=4)"<br />
" Output: akatitles.movieid, akatitles.language, akatitles.title, <akatitles.addition"<br />
" Filter: (((title)::text ~~* '%Babe%'::text) AND ((title)::text !~~* '""%}'::text))"<br />
" -> Index Scan using movies_pkey on movies (cost=0.00..8.44 rows=1 width=46)"<br />
" Output: public.movies.movieid, public.movies.title, public.movies.year, public.movies.imdbid"
" Index Cond: (public.movies.movieid = akatitles.movieid)"<br />
SELECT * FROM (
(SELECT DISTINCT title, movieid, year
FROM movies
WHERE title ILIKE '%Babe%' AND NOT (title ILIKE '"%}'))
UNION
(SELECT movies.title, movies.movieid, movies.year
FROM movies
INNER JOIN akatitles ON movies.movieid=akatitles.movieid
WHERE akatitles.title ILIKE '%Babe%' AND NOT (akatitles.title ILIKE '"%}'))
) AS union_tmp2;
Returns 612 Rows in 9078ms<br />
Database backup (plain text) is 1.61GB
这是一个非常复杂的查询,我不完全了解它就像我说的那样被JMDB吐出来了。
对于如何提高速度你有什么建议吗?
您可以(如果可能的话)在桌面上运行解释分析,以便我们确信成本是否准确?根据查询中的行数来判断,我希望你缺少一个索引。 – Wolph 2010-03-22 05:16:13
再次阅读您的查询后,我预计大部分放缓都会出现在标题ILIKE'%Babe%'部分。通过使用全文索引进行搜索,您可以“很容易”将其提高很多。 – Wolph 2010-03-22 05:20:30
你使用的是什么版本的postgres?去年发布的8.4具有显着的性能改进。 – crowne 2010-03-22 05:26:47