3

嗨亲爱的 postgres 用户,

短篇小说,我有一个与州相关的地址表(每个地址都有一个州)。我将运行以下查询进行搜索:

SELECT "addresses".*, (ts_rank((to_tsvector('simple', coalesce("addresses"."name"::text, '')) || to_tsvector('simple', coalesce(pg_search_85240c410826a2b0e0f0e5.pg_search_442c4ad3183a256248ef8d::text, ''))), (to_tsquery('simple', ''' ' || 'test' || ' ''' || ':*')), 0)) AS pg_search_rank 
FROM "addresses" 
LEFT OUTER JOIN (SELECT "addresses"."id" AS id, string_agg("states"."name"::text, ' ') AS pg_search_442c4ad3183a256248ef8d 
            FROM "addresses" INNER JOIN "states" ON "states"."id" = "addresses"."state_id" GROUP BY "addresses"."id") pg_search_85240c410826a2b0e0f0e5 ON pg_search_85240c410826a2b0e0f0e5.id = "addresses"."id" 
            WHERE (((to_tsvector('simple', coalesce("addresses"."name"::text, '')) || to_tsvector('simple', coalesce(pg_search_85240c410826a2b0e0f0e5.pg_search_442c4ad3183a256248ef8d::text, ''))) @@ (to_tsquery('simple', ''' ' || 'test' || ' ''' || ':*')))) AND (company_id = 2142) 
            ORDER BY pg_search_rank DESC, "addresses"."id" ASC 

直截了当,对吧?这在一台好机器和一个 8581 地址上大约需要 1.226 毫秒。我需要改进这一点,所以我创建了这 2 个索引

CREATE INDEX index_addresses_search_by_name ON addresses USING gin(to_tsvector('simple', COALESCE((public.addresses.name)::text, '')))
CREATE INDEX index_state_search_by_name ON states USING gin(to_tsvector('simple', COALESCE((public.states.name)::text, '')))

这应该会有所帮助,在 Addresses 上创建索引并在 States 上创建索引,但它没有:( 查询和以前一样慢,并且解释显示索引没有被使用。

请提供建议,

编辑:

以下是解释分析将显示的内容:

"Sort  (cost=11.39..11.40 rows=1 width=4824) (actual time=0.947..0.947 rows=0 loops=1)"
"  Sort Key: (ts_rank((to_tsvector('simple'::regconfig, COALESCE((public.addresses.name)::text, ''::text)) || to_tsvector('simple'::regconfig, COALESCE((string_agg((states.name)::text, ' '::text)), ''::text))), '''test'':*'::tsquery, 0)), public.addresses.id"
"  Sort Method: quicksort  Memory: 17kB"
"  ->  Nested Loop Left Join  (cost=6.19..11.38 rows=1 width=4824) (actual time=0.905..0.905 rows=0 loops=1)"
"        Join Filter: (public.addresses.id = public.addresses.id)"
"        Filter: ((to_tsvector('simple'::regconfig, COALESCE((public.addresses.name)::text, ''::text)) || to_tsvector('simple'::regconfig, COALESCE((string_agg((states.name)::text, ' '::text)), ''::text))) @@ '''test'':*'::tsquery)"
"        ->  Seq Scan on addresses  (cost=0.00..5.14 rows=1 width=4792) (actual time=0.904..0.904 rows=0 loops=1)"
"              Filter: (company_id = 2142)"
"        ->  HashAggregate  (cost=6.19..6.20 rows=1 width=520) (never executed)"
"              ->  Hash Join  (cost=1.02..6.18 rows=1 width=520) (never executed)"
"                    Hash Cond: (public.addresses.state_id = states.id)"
"                    ->  Seq Scan on addresses  (cost=0.00..5.11 rows=11 width=8) (never executed)"
"                    ->  Hash  (cost=1.01..1.01 rows=1 width=520) (never executed)"
"                          ->  Seq Scan on states  (cost=0.00..1.01 rows=1 width=520) (never executed)"
"Total runtime: 1.226 ms"

编辑2

地址 ddl

--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: addresses; Type: TABLE; Schema: public; Owner: trucking; Tablespace: 
--

CREATE TABLE addresses (
    id integer NOT NULL,
    name character varying(255),
    street character varying(255),
    city character varying(255),
    zip_code character varying(255),
    primary_phone character varying(255),
    alternate_phone character varying(255),
    fax character varying(255),
    email character varying(255),
    contact character varying(255),
    company_id integer DEFAULT 0 NOT NULL,
    motor_carrier_number character varying(12),
    state_id integer,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    alternate_phone2 character varying(12),
    insurance_expires_on date,
    notes text
);


ALTER TABLE public.addresses OWNER TO trucking;

--
-- Name: addresses_id_seq; Type: SEQUENCE; Schema: public; Owner: trucking
--

CREATE SEQUENCE addresses_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.addresses_id_seq OWNER TO trucking;

--
-- Name: addresses_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: trucking
--

ALTER SEQUENCE addresses_id_seq OWNED BY addresses.id;


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: trucking
--

ALTER TABLE ONLY addresses ALTER COLUMN id SET DEFAULT nextval('addresses_id_seq'::regclass);


--
-- Name: addresses_pkey; Type: CONSTRAINT; Schema: public; Owner: trucking; Tablespace: 
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_pkey PRIMARY KEY (id);


--
-- Name: index_addresses_on_company_id; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_on_company_id ON addresses USING btree (company_id);


--
-- Name: index_addresses_on_state_id; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_on_state_id ON addresses USING btree (state_id);


--
-- Name: index_addresses_search_by_city; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_search_by_city ON addresses USING gin (to_tsvector('simple'::regconfig, COALESCE((city)::text, ''::text)));


--
-- Name: index_addresses_search_by_email; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_search_by_email ON addresses USING gin (to_tsvector('simple'::regconfig, COALESCE((email)::text, ''::text)));


--
-- Name: index_addresses_search_by_name; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_search_by_name ON addresses USING gin (to_tsvector('simple'::regconfig, COALESCE((name)::text, ''::text)));


--
-- Name: index_addresses_search_by_street; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_addresses_search_by_street ON addresses USING gin (to_tsvector('simple'::regconfig, COALESCE((street)::text, ''::text)));


--
-- PostgreSQL database dump complete
--

国家 DDL

--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: states; Type: TABLE; Schema: public; Owner: trucking; Tablespace: 
--

CREATE TABLE states (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    abbrev character varying(255) NOT NULL,
    country character varying(255)
);


ALTER TABLE public.states OWNER TO trucking;

--
-- Name: states_id_seq; Type: SEQUENCE; Schema: public; Owner: trucking
--

CREATE SEQUENCE states_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.states_id_seq OWNER TO trucking;

--
-- Name: states_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: trucking
--

ALTER SEQUENCE states_id_seq OWNED BY states.id;


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: trucking
--

ALTER TABLE ONLY states ALTER COLUMN id SET DEFAULT nextval('states_id_seq'::regclass);


--
-- Name: states_pkey; Type: CONSTRAINT; Schema: public; Owner: trucking; Tablespace: 
--

ALTER TABLE ONLY states
    ADD CONSTRAINT states_pkey PRIMARY KEY (id);


--
-- Name: index_states_search_by_abbrev; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_states_search_by_abbrev ON states USING gin (to_tsvector('simple'::regconfig, COALESCE((abbrev)::text, ''::text)));


--
-- Name: index_states_search_by_name; Type: INDEX; Schema: public; Owner: trucking; Tablespace: 
--

CREATE INDEX index_states_search_by_name ON states USING gin (to_tsvector('simple'::regconfig, COALESCE((name)::text, ''::text)));


--
-- PostgreSQL database dump complete
--

迄今为止最好的重写

SELECT consolidated_address.id, (ts_rank((to_tsvector('simple', coalesce(consolidated_address.name::text, '')) || to_tsvector('simple', coalesce(consolidated_address.state_name::text, ''))), (to_tsquery('simple', ''' ' || 'Gallaway' || ' ''' || ':*')), 0)) AS pg_search_rank 
FROM (
 SELECT "addresses".id,
    "addresses".name,
    string_agg("states".name::text, ' ') as state_name
    FROM addresses
    LEFT OUTER JOIN "states"
    ON "states".id = "addresses".state_id
    GROUP BY "addresses".id) consolidated_address
WHERE 
    (((to_tsvector('simple', coalesce(consolidated_address.name::text, '')) || to_tsvector('simple', coalesce(consolidated_address.state_name::text, ''))) @@ (to_tsquery('simple', ''' ' || 'Gallaway' || ' ''' || ':*')))) 

它有点快,但仍然不使用任何索引

谢谢,

4

3 回答 3

1

我对您的解释分析输出的阅读是您的表太小而无法从使用索引中获利。PostgreSQL 上的顺序扫描相对便宜,因为表是作为物理文件加载的,并且 PostgreSQL 可以利用操作系统预取等。

请记住,没有任何计划比从磁盘加载单个页面并按顺序扫描它更快。使用索引只会增加开销。顺序扫描不需要花费任何时间,因此在您拥有真实数据之前不要担心。

值得注意的是,在 PostgreSQL 上,在获得数据之前优化查询是导致问题多于解决方案的秘诀。我真诚的建议是等待分析查询,直到你有一个真实的返回真实数据。然后我们可以确定哪些索引会有所帮助。

编辑:你说查询很慢。但计划显示它在 1 毫秒内执行。你预计需要多长时间?

于 2012-09-30T01:25:19.913 回答
1

从 Github 交叉发布的答案https://github.com/Casecommons/pg_search/issues/51

我是生成此查询的pg_search Ruby gem 的作者。


是的,不幸的是,我不知道如何让 :associated_against 查询对索引起作用,至少目前实现的方式是这样。

这是因为 :associated_against 搜索所有关联记录的文本,而不是一次搜索单个记录。

因此,例如,如果您加入了一个标签表,并且有 3 条记录(“foo”、“bar”和“baz”),那么您会期望搜索“foo baz”来找到它。更容易索引的解决方案只适用于“foo”或“baz”查询,但不适用于“foo baz”,因为没有一个单独的标签行与这两个术语匹配,如果你知道我的意思的话。

不可能跨多个记录建立索引(至少据我所知)。

也许我们可以在 pg_search 中显示一个选项来逐条搜索记录,它可以使用索引,但不能跨记录匹配。

于 2012-08-14T17:48:50.487 回答
0

This query is slow because the query you are joining to via left outer join is using aggregating the entire address table then restricting by company_id. Does the address table have a company_id? Try moving the where clause to the inner query to restrict the number of records returned.

于 2012-08-13T23:42:17.587 回答