连接列上的索引未在查询中使用

时间:2017-10-18 12:59:17

标签: sql postgresql indexing

我在表达式上创建了以下索引:

CREATE INDEX ON administration((client_name || ' - ' || project_name));

当我尝试执行使用此表达式的查询时,结果有些缓慢。查询是:

SELECT client_name || ' - ' || project_name FROM administration WHERE 
(client_name || ' - ' || project_name LIKE 'AT&T%');

\d administration我可以看到:

...
"projects_expr_idx" btree (((client_name::text || ' - '::text) || project_name::text))
...

但在查询中使用EXPLAIN ANALYZE,结果似乎不会使用创建的索引:

                                    QUERY PLAN                                              
------------------------------------------------------------------------------------------------------
Seq Scan on projects  (cost=0.00..142.40 rows=1 width=32) (actual 
time=0.036..1.874 rows=60 loops=1)
Filter: ((((client_name)::text || ' - '::text) || (project_name)::text) ~~ '%AT&T%'::text)
   Rows Removed by Filter: 3677
Planning time: 0.214 ms
Execution time: 1.889 ms
(5 rows)

我错了吗?或者查询不需要使用创建的索引。谢谢你的帮助。

另一个也是最后一个问题,假设索引正在运行,此查询将使用它吗?

SELECT projects 
    FROM (
        SELECT 
            CASE
              WHEN left(project_name, 2) = ' -' THEN 
                client_name || project_name 
              ELSE 
                client_name || ' - ' || project_name 
            END AS projects 
        FROM administration) 
    WHERE projects LIKE '%AT&T%' 
    ORDER BY projects ASC 
    LIMIT 100;

0 个答案:

没有答案