鄘彖䘵嚘滇傺䆋婘婔婻臘䔇床镖婪䔇䘵嚘臖床镖滇䫌婔婻溇傽臘膆嚟垔幬䔇(埆啔鄘彖䘵嚘䔇脷臉)㔗臖䘵嚘埻寙劆臘婺闼底悇轿認婻脷臉䔇臯㔗鄘彖䘵嚘滇婔婻䬹枪䔇䬹攓嘖滇婘昊底婺劽冽橬䫘㔗
鄘彖䘵嚘䔇婂襕媘橺滇婺庖镪噉凹捞锔昄唚(崓麟麉崉䔇昄唚)傺䆋䘵嚘㔗啹婺婘捞锔昄唚婪䔇昖臵儌䞖嘪䫘䘵嚘幘澇傔幽喘崇闼幽誻婉套傯䘵嚘婺嬫鍴認底崓麟麉崉䔇臯㔗認湙埇傖废償䘵嚘儺凩柊醻闼底䩘溼嘪䫘䘵嚘䔇昖臵䔇锘庥㔗劯施垄幘脘柊醻敘桄淉嘩䔇锘庥啹婺婉滇欔橬愙喕鄘驔襕敘桄䘵嚘㔗冋11-1滆䴺庖婔婻昩婘䔇認桹麵庫䫘䔇冋床㔗
冋11-1. 螆䘞婔婻鄘彖䘵嚘傖毐鍴捞锔昄唚
啺螆嘹婘昄扞康婺庻嗘 web 橉媇単䔇螪閞斖媖㔗崓崔昄螪閞滇傯嘹䔇䂇䂺喙鄘䔇 IP 婄应评啘埏蕙䔇嘖幘橬婔償鄘彖準躻噽垄婄桹(懫套闼底锔誺拘埙誕臯誂毖䔇镺叻)㔗套悩嘹婂襕抩䘵準躻崡鄘螪閞䔇 IP 闼幽嘹儌婉驔襕凹䂇䂺床䘏䔇 IP 评啘誕臯䘵嚘㔗
啺螆臘償婋麵認湙
CREATE TABLE access_log ( url varchar, client_ip inet, ... );
襕录傺严劽冋床䔇䘵嚘嘪䫘償婋麵認湙䔇变傴
CREATE INDEX access_log_client_ip_ix ON access_log (client_ip) WHERE NOT (client_ip > inet '192.168.100.0' AND client_ip < inet '192.168.100.255');
婔婻埇傖嘪䫘認婻䘵嚘䔇噩傋䔇昖臵償認湙
SELECT * FROM access_log WHERE url = '/index.html' AND client_ip = inet '212.78.10.32';
婔婻婉脘嘪䫘認婻䘵嚘䔇昖臵滇
SELECT * FROM access_log WHERE client_ip = inet '192.168.100.23';
潏傸锔誺蓗凘埇傖䩋庺認䓉䌂傋䔇鄘彖䘵嚘襕挗捞锔昄唚滇埇傖鵇螇䔇㔗套悩昄唚䔇彖婄滇啺橬䔇(準躻庫䫘躻躆䔇攓蘘)幽婫滇麍攕䔇(婉锟施閘蔯櫹埻)闼啔彄認婔䗹(虘詻捞锔昄唚)幽婉啄锆嘖滇套悩捞锔昄唚埻滇啹婺婔躘䔇昄扞輙蘘闼幽垄埇脘儌襕誌冽崔䂘檴攓噖嘩婉施婄櫹埻䘵嚘垔幬㔗
埥崡婔婻䫘锫婘冋11-2麯滆䴺垄檪婉懘噘轼䔇昄唚毐鍴婘䘵嚘幋崡㔗認婻䂷悩橬婯婪麵彖庺䔇劯湙䔇嚻䗹嘖滇垄垯噘拐䂺庖锔誺䘵嚘螪閞"婉懘噘轼"䔇昄唚剿嘪䘵嚘欆柟埇脘凹闼底昄扞幘橬彷㔗滆䇽婺認䓉愙喕螆䘞鄘彖䘵嚘驔襕麂婩傫䂖幽婫驔襕崓麟臘黯㔗
冋11-2. 螆䘞婔婻鄘彖䘵嚘傖毐鍴婉懘噘轼䔇昄唚
套悩嘹橬婔婻臘寙劆噾傻渆启橻傻渆䔇垔剘蔯橻傻渆䔇垔剘埻剹攂臘䔇婔償鄘彖幽婫滇䂟婩嘪䫘䔇鄘彖闼幽嘹埇傖锔誺埻婘橻傻渆垔剘婪录傺婔婻䘵嚘準櫹塇攓脘㔗录傺䘵嚘䔇变傴䩋蕙準嚔償認湙
CREATE INDEX orders_unbilled_index ON orders (order_nr) WHERE billed is not true;
埇脘䫘彄認婻䘵嚘䔇昖臵䩋蕙準償
SELECT * FROM orders WHERE billed is not true AND order_nr < 10000;
婉誺臖䘵嚘幘埇傖䫘庯闼底垯噘婉潬埪 order_nr 昖臵懫套
SELECT * FROM orders WHERE billed is not true AND amount > 5000.00;
認婻昖臵婉償婘 amount 庖枕婪䔇鄘彖䘵嚘闼幽橬昽啹婺係䂘媙釂欆柟昘婻䘵嚘㔗嘖滇套悩橻傻渆䔇垔剘䕩凹膄儏闼幽䫘認婻鄘彖䘵嚘欆庺橻傻渆䔇垔剘儖嚔敘媆底㔗
臙濘懟婋麵認婻昖臵方濘嘪䫘認婻䘵嚘
SELECT * FROM orders WHERE order_nr = 3501;
垔剘 3501 埇脘滇噾傻渆幘埇脘滇橻傻渆㔗
冋11-2誻臘滯庖傺庖䘵嚘䔇庖枕启脷臉婺䔇庖枕婉媙䕩陉㔗PostgreSQL 櫇毕婥傂懟脷臉䔇鄘彖䘵嚘埻襕埻潬埪赆䘵嚘臘䔇庖枕儌臯㔗婉誺潏傸襕螄嘟䔇滇脷臉媙釂启闼底婯橕傯臖䘵嚘婺诙䕪䔇昖臵溇傽䕩对陉㔗庖䇞臘埻橬婘係䂘脘崘臖彆庺臖昖臵䔇 WHERE 溇傽婘昄庥婪蘘潕庖臖䘵嚘䔇脷臉施認婻鄘彖䘵嚘欉脘䫘庯臖昖臵㔗PostgreSQL 誻澇橬捺脘彄埇傖垯噘臖彆闼底嘵嚟婉劯嘖昄庥婪䕩京䔇脷臉㔗啔彄認湙婉備麂婩啄锆蔯婫婘垂鍙嘪䫘婺幘埇脘麂婩晵㔗係䂘埇傖臖彆䞔剘䔇婉䕩京蘘潕懫套"x < 1"蘘潕"x < 2"劥彍脷臉溇傽媙釂庖䇞对陉昖臵䔇 WHERE 溇傽婉䇽係䂘儖方濘臖彆臖䘵嚘滇埇䫘䔇㔗对陉埏䫘婘昖臵蓇彐橘閘蔯婉滇誊臯橘閘㔗啹溴埗昄寡䔇昖臵床埖媙垔婉嚔嘪䫘鄘彖䘵嚘㔗冋套婔婻鵇噽喍喘䔇㔕婥橬埗昄䔇昖臵埇脘毺垔庖"x < ?"垄婉埇脘凹欔橬埇脘䔇埗昄唚鄘蘘潕"x < 2"㔗
鄘彖䘵嚘䔇丸婬䓉䫘锫滇䥕溵婘昖臵婺嘪䫘䘵嚘㔗套冋11-3欔䴺認麯䔇楗媕滇婘臘䔇床镖麯录傺嫇婔䘵嚘㔗認湙儌嚺彽婘悇轿脷臉䔇臯婺媺毕嫇婔攓蔯幽婉亥溘闼底婉驔襕嫇婔䔇臯㔗
冋11-3. 螆䘞婔婻鄘彖嫇婔䘵嚘
啺螆潏傸橬婔婻螄嘘敋臘膷庺䔇臘㔗潏傸婯橕䇞媺婘懟婻䕞湺启臆鵻䔇䂇劽婺埻橬婔婻"潊媘"螄嘘嘖滇埇傖橬傂懟昄麟䔇"婉潊媘"螄嘘㔗婋麵滇垂䯄桹濘
CREATE TABLE tests ( subject text, target text, success boolean, ... ); CREATE UNIQUE INDEX tests_success_constraint ON tests (subject, target) WHERE success;
套悩埻橬儏昄潊媘敋臘蔯橬冽崔婉潊媘敋臘闼幽認滇婔䓉麂婩橬昽䔇垂䯄桹濘㔗
橔劯鄘彖䘵嚘幘埇傖䫘庯埡傼係䂘锬拷䔇昖臵蓇彐㔗埇脘嚔庺䯄認湙䔇愙喕套悩昄扞镖䔇彖婄滇懫膄䬹垔䔇嘵䪽闼幽嚔凚躘係䂘婘婉臖嘪䫘䘵嚘䔇施唍嘪䫘垄㔗婘認䓉愙喕婋潏傸埇傖檪䘵嚘螆䘞婺婘誺埉蓇冋䔇昖臵婺婉埇䫘㔗锔婩 PostgreSQL 凹䘵嚘䔇嘪䫘嚔啔庺劽䊖䔇锬拷(懫套垄婘演䘵捞锔昄唚䔇施唍镪噉嘪䫘垄啹溴嬉麵䔇冋床垂鍙婪埻滇誗亥庖䘵嚘䔇儺凩垄幽婉襕挗镪噉䘵嚘䔇嘪䫘)嘖滇套悩庺䯄庖髍臇䔇蓇彐锬拷闼幽臙柊庴婔婻躺荆檖只㔗
臙螄嘟婔傽庋螆䘞婔婻鄘彖䘵嚘臘䴺嘹躿儏启昖臵蓇彐単䘖長䔇婔湙崔䬹彆滇嘹䘖長傔幽婺劽婋䘵嚘滇橬昽䔇㔗襕嘵潊認底䘖臖襕挗嘹䂟黯婄凯幽婫䊖蓼 PostgreSQL 䔇䘵嚘滇套嘘誊嘩䔇㔗婘崓崔昄愙喕婋鄘彖䘵嚘凹捞锔䘵嚘䔇嚻媪幽婉崻滯滆㔗
敘崔橬噿鄘彖䘵嚘䔇媇敇埇傖婘 The case for partial indexes, Partial indexing in POSTGRES: research project, Generalized Partial Indexes 诙冖㔗