涉及空值的多列索引

时间:2017-04-21 17:13:22

标签: postgresql

我有一个where子句

git log --oneline

和索引WHERE (v.regstnum = voters.regstnum OR voters.regstnum IS NULL) AND (v.regstfrac = voters.regstfrac OR voters.regstfrac IS NULL) AND (v.regstname = voters.regstname OR voters.regstname IS NULL) AND (v.regsttype = voters.regsttype OR voters.regsttype IS NULL) AND (v.regstpredirection = voters.regstpredirection OR voters.regstpredirection IS NULL) AND (v.regstpostdirection = voters.regstpostdirection OR voters.regstpostdirection IS NULL) AND (v.regunittype = voters.regunittype OR voters.regunittype IS NULL) AND (v.regunitnum = voters.regunitnum OR voters.regunitnum IS NULL) AND (v.regcity = voters.regcity OR voters.regcity IS NULL) AND v.fname != voters.fname)

但根据解释,该指数并未被使用。

我的猜测是由于我必须添加的IS NULL部分引起的,因为我有许多空值,并且查询在没有IS NULL部分的情况下也没有工作。

那么如何重写我的查询以处理空值并使用索引?

表格结构:

"address2" btree (regstnum, regstfrac, regstname, regsttype, regstpredirection, regstpostdirection, regunittype, regunitnum, regcity)

说明:

         Column         | Type  | Modifiers
------------------------+-------+-----------
 slug                   | text  | not null
 voters_at_same_address | jsonb |
 statevoterid           | text  |
 countyvoterid          | text  |
 title                  | text  |
 fname                  | text  |
 mname                  | text  |
 lname                  | text  |
 namesuffix             | text  |
 birthdate              | date  |
 gender                 | text  |
 regstnum               | text  |
 regstfrac              | text  |
 regstname              | text  |
 regsttype              | text  |
 regunittype            | text  |
 regstpredirection      | text  |
 regstpostdirection     | text  |
 regunitnum             | text  |
 regcity                | text  |
 regstate               | text  |
 regzipcode             | text  |
 countycode             | text  |
 precinctcode           | text  |
 precinctpart           | text  |
 legislativedistrict    | text  |
 congressionaldistrict  | text  |
 mail1                  | text  |
 mail2                  | text  |
 mail3                  | text  |
 mail4                  | text  |
 mailcity               | text  |
 mailzip                | text  |
 mailstate              | text  |
 mailcountry            | text  |
 registrationdate       | date  |
 absenteetype           | text  |
 lastvoted              | date  |
 statuscode             | text  |
 dflag                  | text  |
Indexes:
    "voters_pkey" PRIMARY KEY, btree (slug)
    "address" btree (regstnum, regstfrac, regstname, regsttype, regunittype, regstpredirection, regstpostdirection, regunitnum, regcity, regstate, regzipcode)
    "address2" btree (regstnum, regstfrac, regstname, regsttype, regstpredirection, regstpostdirection, regunittype, regunitnum, regcity)
    "names" btree (fname, lname)

完整查询:

Index Scan using names on voters  (cost=0.43..840447.35 rows=3 width=58)
   Index Cond: ((fname = 'MICHAEL'::text) AND (lname = 'BEEBE'::text))
   SubPlan 1
     ->  Aggregate  (cost=280143.59..280143.60 rows=1 width=17)
           ->  Seq Scan on voters v  (cost=0.00..280143.56 rows=1 width=17)
                 Filter: (((regstnum = voters.regstnum) OR (voters.regstnum IS NULL)) AND ((regstfrac = voters.regstfrac) OR (voters.regstfrac IS NULL)) AND ((regstname = voters.regstname) OR (voters.regstname IS NULL)) AND ((regsttype = voters.
regsttype) OR (voters.regsttype IS NULL)) AND ((regstpredirection = voters.regstpredirection) OR (voters.regstpredirection IS NULL)) AND ((regstpostdirection = voters.regstpostdirection) OR (voters.regstpostdirection IS NULL)) AND ((regunittype
= voters.regunittype) OR (voters.regunittype IS NULL)) AND ((regunitnum = voters.regunitnum) OR (voters.regunitnum IS NULL)) AND ((regcity = voters.regcity) OR (voters.regcity IS NULL)) AND (fname <> voters.fname))
(6 rows)

0 个答案:

没有答案