我有PostgreSQL和数据库,在数据库中我有一个表,其结构类似于以下结构:
ID (uuid) | User (xml) | Details (xml)
我想在User和Details列上使用XPATH执行group by
操作。所以我创建了两个索引如下:
create index idx_detail1
on mytable
using gin(cast(xpath('//Details/Detail/[@Name="Detail1"]', "Details") as text[]))
create index idx_user
on mytable
using gin(cast(xpath('//UserId/@Value', "User") as text[]))
然后,我执行以下select
语句:
select (cast(xpath('//UserId/@Value', "User") as text[]))[1], (cast(xpath('//Details/Detail/[@Name="Detail1"]', "Details") as text[]))[1], count(*)
from mytable
group by (cast(xpath('//UserId/@Value', "User") as text[]))[1], (cast(xpath('//Details/Detail/[@Name="Detail1"]', "Details") as text[]))[1]
问题是Postgres没有使用我创建的索引,并坚持在表上执行顺序扫描而不是使用索引。
如何让Postgres使用group by
操作的索引?