PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹33. 欷匘 SQL媆誕嬉誕

33.13. 淉嘩严嚻寡媇敇

PostgreSQL 䔇淉嘩严垔幬埇傖寙拸庹婻埇锬䔇床埖認底床埖只臬係䂘婔底噿庯臖淉嘩严䬹攓䔇橬䫘媇敇㔗婘埇脘䔇愙喕婋鄘庫臖柊冕認底床埖啹婺垄傸埇脘婺嘪䫘認婻淉嘩严䔇昖臵婥準埇蓗䔇锘庥柊剺㔗婉誺襕濘懟套悩嘹弄滯庖認底床埖儌媙釂䇞媺垄傸滇溼䇞䔇凹嚻寡床埖䔇髍臇嘪䫘儖凚躘橉媇単䔇嘷溄㔕冞償䔇膷庺髍臇㔕潡蔙噽垄俘俘庋愙㔗套悩嘹凹認底庋愙婉䇞垔䔇臺埇傖攂滇媘䘖嚻寡床埖嫇婔䔇劯悩儌滇昖臵埇脘誊臯䔇晵婔底㔗

鍇媹䔇嚻寡床埖埇脘婘傪劯䔇 PostgreSQL 䬽橸麯嵂媹㔗認麯柟誄䔇鄘滇 8.2.3 䬽橸埇傖䊖蓼䔇㔗

33.13.1. COMMUTATOR

套悩柊冕庖 COMMUTATOR 床埖彍变劉婔婻淉嘩严滇赆垔幬䔇淉嘩严䔇庴扵严㔗套悩橬婴婻淉嘩严 A, B 凹庯傂嘘埇脘䔇膷噖昄唚 x, y 鄘橬(x A y)京庯(y B x)闼幽儌臘 A 滇 B 䔇庴扵严劯湙 B 幘滇 A 䔇庴扵严㔗冋套淉嘩严 <> 凹庯欔嘪䫘䔇婔垔䔇昄扞䌂傋锔婩鄘滇凹桹䔇庴扵严蔯淉嘩严 + 锔婩滇垄躻躆䔇庴扵严㔗嘖滇淉嘩严 - 锔婩澇橬庴扵严㔗

庴扵淉嘩严䔇噥淉嘩昄婯埿淉嘩昄䌂傋媙釂䕩劯㔗欔傖 PostgreSQL 欔驔襕䔇埻滇婔婻庴扵严淉嘩严䔇劉䓄䫘傖昖欆臖庴扵严闼幘滇 COMMUTATOR 床埖麯欔驔襕䔇嫇婔䔇婩薪㔗

䂍闼底嚔婘䘵嚘启誂毖床埖麯麵嘪䫘䔇淉嘩严柊冕庴扵严滇麂婩噿髞䔇啹婺認湙儌噕螩昖臵嚻寡単"䓂媘"認湙䔇床埖嘵潊欔驔襕䔇婉劯䔇蓇彐䌂傋䔇嘵嚟㔗懫套蔄荏婔婻橬䌂嚚 tab1.x = tab2.y 䔇 WHERE 床埖䔇昖臵認麯 tab1.xtab2.y 滇䫘潙垔幬䌂傋幽婫啺螆 tab2.y 婪麵橬䘵嚘㔗鍴麂嚻寡単䘖長套嘘婘 tab2.y = tab1.x 变啘啕崇䓂媘臖床埖劥彍垄婉脘䫘潊䘵嚘欆柟啹婺䘵嚘欆柟橺彽橘橕䩋彄䘵嚘庖枕婘䂍庺䔇淉嘩严噥膹㔗PostgreSQL 婉嚔䞔剘婄啺螆認滇婔婻劽濘䔇蘸扵= 䔇录傺蔙媙釂弄滯認滇橬昽䔇桹濘滇䂍認婻淉嘩严湺螄庴扵単媇敇㔗

嘷嘹垔幬婔婻躻庴扵䔇淉嘩严施嘹䞔剘䔇垔幬垄儌埇傖庖㔗嘷嘹垔幬婔凹庴扵严淉嘩严施庋愙儌橬婔䗹漻欋支湙垔幬婔婻淉嘩严䔇庴扵严毺劏埥婔婻嘹誻澇橬垔幬䔇淉嘩严叵? 凹認婻閞鵻橬婴婻蓼喿桹濘

33.13.2. NEGATOR

套悩柊冕庖 NEGATOR 床埖彍变劉婔婻淉嘩严滇赆垔幬䔇淉嘩严䔇劥垔严㔗套悩橬婴婻鄘誫啂婄儫埻麟䔇淉嘩严 A 启 B 凹傂嘘埇脘䔇膷噖 x 启 y 鄘橬 (x A y) 京庯 NOT(x B y)闼幽臘 A 滇 B 䔇劥垔严㔗嘷䇽 B 幘滇 A 䔇劥垔严㔗冋套<>= 凹崓崔昄昄扞䌂傋滇婔凹劥垔严㔗婔婻淉嘩严婉埇脘滇垄躻躆䔇劥垔严㔗

婉償庴扵严婔凹剘䕞淉嘩严埇傖庐婺劥垔严闼儌懟叿五凹庯欔橬䔇 x 鄘橬 (A x) 京庯 NOT(B x) 潡蔙䌂嚚䔇埿䕞淉嘩严䔇認䓉愙喕㔗

婔婻淉嘩严䔇劥垔严媙釂橬婯溼垔幬䔇淉嘩严橸躆婔湙䔇噥启/潡埿淉嘩昄欔傖儌償 COMMUTATOR 婔湙埻橬淉嘩严劉驔襕婘 NEGATOR 床埖麯麵䂍庺㔗

柊冕劥垔严凹昖臵嚻寡単滇麂婩橬婞媷䔇啹婺認湙儌噕螩償 NOT (x = y) 認湙䔇臘膆嚟䞔寡潊 x <> y 㔗認湙䔇愙喕懫嘹愿償䔇襕鵏乕䔇崔啹婺 NOT 埇脘啹婺噽垄䔇麉毐彖蔯赆嚘噖㔗

劥垔严凹埇傖䫘婪麵庴扵严凹婺蓼麪䔇䕩劯䔇桹濘準垔幬㔗

33.13.3. RESTRICT

套悩柊冕庖 RESTRICT 床埖彍婺淉嘩严变劉婔婻锬拷攓鍊彽螇䞖庘昄(濘懟認麯滇婔婻庘昄劉蔯婉滇婔婻淉嘩严劉)㔗RESTRICT 床埖埻滇凹誫啂 boolean 埻麟䔇埯䕞淉嘩严橬懟幬㔗锬拷攓鍊彽螇䞖严䔇楗媕滇䯩敋婔婻臘婺欔橬臯䔇巻婔鄘彖凹庯䕞嬉䔇淉嘩严启䬹垔䔇婩麟儖悇轿婔婻償婋麵認湙嘵嚟䔇 WHERE 溇傽床埖㔗

column OP constant

垄埇傖䂍庺認䓉䌂傋䔇 WHERE 床埖埇傖役鍴崔儏臯䔇婔婻楗媕認儖婞媷嚻寡単誕臯嚻寡㔗嘹埇脘嚔臘套悩臖婩麟(constant)婘噥膹支幽媂?工闼滇 COMMUTATOR 幾䔇庋...

幥喍桄䔇锬拷攓鍊彽螇䞖庘昄誩誩轙庺庖橸䆹䔇评啘婉誺冽幩誊䔇滇锔婩嘹凹躻噌䔇淉嘩严埻驔襕嘪䫘係䂘湺庖䔇螇䞖単幋婔儌臯庖㔗婋麵滇婔底湺庖鍊彽螇䞖単

eqsel 䫘庯 =
neqsel 䫘庯 <>
scalarltsel 䫘庯 <<=
scalargtsel 䫘庯 >>=
認底鄘滇彖䌂䩋蕙準橬䗹喺攻婉誺套悩嘹傫䂖愿愿儌嚔蓬冖橬長䊖㔗= 崓崔儖埻毖埖臘婺䔇婔償鄘彖臯<> 崓崔儖拐䂺婔償鄘彖臯㔗< 毖埖䔇臯埡喿庯䂍庺䔇婩麟芘婘臘䔇臖彖昄扞唚䔇巻婔婻评啘麯(臖唚䵄噓滇 ANALYZE 櫽镖幽婫柊冕䂍锬拷攓螇䞖単䔇媇敇)㔗<= 婘劯湙䔇婩麟施嚔毖埖懫 < 䘖冞崓婔底䔇臯婉誺垄傸幘麂婩毖誏庹幯婉唚冖寺彆嚔準儴噽滇方螺套嘘幘懫啔䕾䯩喘冖崔㔗䌂嚚䔇愙喕幘锗䫘庯 >>=

嘹埇脘婩幹愇庯檪 eqselneqsel 䫘庯闼底麂婩醻潡蔙麂婩嘯锬拷攓䔇淉嘩严剿嘪垄傸幽麂䩘溼䕩京潡蔙婉䕩京㔗冋套嘺庯埻嚔对陉昘婻臘婺婔償鄘彖螄嘘䔇啺螆庹嘘淉嘩严亥京庯儌嘪䫘 eqsel

嘹埇傖檪 scalarltselscalargtsel 䫘庯懫膄闼底婺誕臯评啘懫膄赆蘸寡婺昄庖儺庥劯橬滯滆懟幬䔇昄扞䌂傋㔗套悩埇脘檪臖昄扞䌂傋嵂媹彄埇傖赆 src/backend/utils/adt/selfuncs.c 桺傽麯䔇 convert_to_scalar() 庘昄䊖蓼䔇鄘彖㔗橔䂽認婻誺䘋儖赆櫆彄䫌 pg_type 臘麯䔇婔婻彖湺臖䔇懟䓉䌂傋婔婻䔇庘昄傼敪婉誺䕞嬉誻澇橬認幽啔㔗套悩嘹澇橬啔認底係䂘傉䇽脘噖嘩婉誺嚻寡単䔇嚄螇婉嚔償愿償䔇闼幽喘㔗

src/backend/utils/adt/geo_selfuncs.c 麯誻橬婺庹嘘淉嘩严螆螇䔇鵺崡锬拷攓臇嚄庘昄areasel, positionsel, contsel 㔗䕞嬉垄傸鄘埻滇庻湹嘖滇嘹誻滇埇傖嘪䫘(橔劯滇櫹謇)垄傸㔗

33.13.4. JOIN

套悩柊冕庖 JOIN 床埖彍婺淉嘩严变劉婔婻誂毖锬拷攓庘昄(滇庘昄劉婉滇淉嘩严劉)㔗JOIN 床埖埻滇凹誫啂 boolean 䔇埯䕞淉嘩严橬懟幬㔗婔婻誂毖锬拷攓螇䞖単劯麵䔇楗媕滇䯩敋婔凹臘婪䔇巻鄘彖臯凹䕞嬉䔇淉嘩严儖悇轿婋麵嘵嚟䔇 WHERE 床埖䔇溇傽

table1.column1 OP table2.column2

RESTRICT 床埖婔湙認底冽橬埇脘婞媷嚻寡単䫘橔儏䔇崇䊖勆䫂庺襕麺埡埇脘䔇誂毖釺废婺䔇巻婔婻㔗

启嬉麵婔湙橸誗婉嚔臘商蓼麪套嘘幥喍婔婻誂毖锬拷攓螇䞖単庘昄嘖滇嚔傺螞嘹儘埇脘嘪䫘婔婻湺庖䔇螇䞖単

eqjoinsel 䫘庯 =
neqjoinsel 䫘庯 <>
scalarltjoinsel 䫘庯 <<=
scalargtjoinsel 䫘庯 >>=
areajoinsel 䫘庯嘺庯麵䓇䔇庯䂘懫膄
positionjoinsel 䫘庯嘺庯嘉䘞䔇庯䂘懫膄
contjoinsel 䫘庯嘺庯寙劆䔇庯䂘懫膄

33.13.5. HASHES

套悩庺䯄庖 HASHES 床埖彍只臬係䂘凹庯婔婻嘺庯溴淉嘩严䔇誂毖埇傖嘪䫘 Hash 誂毖㔗HASHES 埻凹誫啂 boolean 䔇埯䕞淉嘩严橬懟幬幽婫垂鍙婪臖淉嘩严橔喘滇凹昊䓉昄扞䌂傋䔇䕩京淉嘩严㔗

Hash 誂毖䔇啺螆滇凹庯婔凹昼彖彄劯湙䔇 Hash 傼乕䔇噥启埿淉嘩昄唚臖誂毖淉嘩严埻脘誫啂䩘㔗套悩婴婻唚赆櫆彄婉劯䔇 Hash 懽麯誂毖儖湹橸婉懫膄垄傸锊劆婄懟叿五誂毖淉嘩严䔇䂷悩婔垔滇啺㔗欔傖凹庯婉傼臘䕩京䔇淉嘩严弄滯 HASHES 滇澇橬懟幬䔇㔗

襕湺螄婺 HASHES 誂毖淉嘩严媙釂庺䯄婘婔婻 Hash 䘵嚘淉嘩严䌂婺㔗婘录傺淉嘩严施幽婉嚺彽認湙啹婺嚘䫘淉嘩严䌂婉埇脘噾䂟庻婘㔗嘖滇嚕商婘 Hash 誂毖婺嘪䫘儔婉庻婘䔇淉嘩严儖婘誊臯施凚躘崌蘖㔗係䂘驔襕淉嘩严䌂湹扞淉嘩严䔇膷噖昄扞䌂傋䇞垔䬹垔庯臖昄扞䌂傋䔇 Hash 庘昄㔗嘷䇽嘹媙釂婘录傺淉嘩严䌂幋嬉饡噽柊冕劽锗䔇 Hash 庘昄㔗

婘䚡喍 Hash 庘昄施媙釂償媄啹婺橬婔底䇸傽䕩噿䔇啹䘹嚔凚躘髍臇㔗懫套套悩嘹䔇昄扞䌂傋滇婔婻庻婘閘锍䔇䂷悇嘷嘹儌婉脘䞔剘䔇儖噽嚹锐䂍昊婻 hash_any 庘昄㔗鍴麂嘹䔇噽垄淉嘩严脘崘䇞媺認底閘锍攂滇镽(認滇傺螞䔇亡䘖)㔗埥婔婻冋床滇婘严劽 IEEE 敞䗹湺庖䔇橺単婪蘘镽启溼镽滇婉劯䔇唚(婉劯䔇嘉昇嚟)嘖滇垄傸赆垔幬婺懫膄䕩京㔗套悩婔婻敞䗹唚埇脘寙劆蘘镽潡溼镽闼幽媙釂嘪䫘鵺崡䔇準黴準䇞媺婴蔙库䫘䕩劯䔇 Hash 唚㔗

㔊濘懟㔏婘婘婔婻埇 Hash 誂毖䔇淉嘩严婋北䔇庘昄媙釂 immutable 潡 stable 㔗套悩垄滇 volatile 闼幽係䂘儖傯婉婘 Hash 誂毖婺嘪䫘認底淉嘩严㔗

㔊濘懟㔏套悩婔婻埇 Hash 誂毖䔇淉嘩严橬婔婻婋北庘昄湺螄婺婖湚䔇(strict)闼幽臖庘昄媙釂垯昘幘儌滇臘凹庯傂嘘麂 NULL 膷噖垄庫臖誫啂 TRUE 潡 FALSE 嘖䂺婉脘滇 NULL 㔗套悩婉镕冻認婻蓇彍IN 淉嘩䔇 Hash 嚻寡埇脘嚔䫘潊髍臇䔇䂷悩㔗䬹彆滇湹扞蓇评溼䇞享懽滇 NULL 䔇施唍IN 埇脘嚔誫啂 FALSE 潡蔙垄埇脘䫘潊婔婻髍臇檌攘臘垄凹 NULL 䂷悩澇橬攺愿庖崺㔗

33.13.6. MERGES (SORT1, SORT2, LTCMP, GTCMP)

套悩庺䯄庖 MERGES 床埖彍只臬係䂘凹嘺庯䕞嬉䔇淉嘩严埇傖嘪䫘肉劽誂毖桹濘㔗MERGES 埻滇凹誫啂 boolean 䔇埯䕞淉嘩严橬懟幬垂鍙婪認婻淉嘩严凹庯昊底昄扞䌂傋潡蔙昊凹昄扞䌂傋媙釂臘䴺䕩京㔗

肉劽誂毖滇傖認湙䔇楗媕婺嘺䇔䔇凹噥膹启埿膹䔇臘誕臯毐废䇽劯幽埏婄欆柟垄傸㔗欔傖婴䓉昄扞䌂傋鄘媙釂滇脘崘垯噘毐废䔇幽婫誂毖淉嘩严媙釂埻凹闼底芘婘毐废釺废婺䔇"昊婻嘉䘞"䔇昄唚凹潊媘㔗垂鍙婪認懟叿五誂毖淉嘩严媙釂臘䯄冖償京庯㔗嘖滇启 Hash 誂毖婉劯Hash 誂毖麯噥膹启埿膹䔇昄扞䌂傋橔喘滇䕩劯䔇(躿儏滇毬嘉䕩京)埇傖凹婴䓉婉劯昄扞䌂傋誕臯肉劽誂毖(埻襕傡傸锂膏䕩京剿埇)㔗冋套smallintinteger 䔇䕩京淉嘩严滇埇傖䫘肉劽誂毖䔇㔗埻驔襕埇傖檪婴䓉昄扞䌂傋毐彖潊锂膏埇懫废彖䔇毐废淉嘩严剿埇㔗

肉劽誂毖䔇欓臯襕挗係䂘埇傖湺臖啕䓉婯肉劽誂毖䕩京攓淉嘩严䕩噿䔇淉嘩严䫘庯噥淉嘩昄昄扞䌂傋䔇償庯懫膄䫘庯埿淉嘩昄昄扞䌂傋䔇償庯懫膄婘婴䓉昄扞䌂傋幋閘䔇償庯懫膄傖埪婘婴䓉昄扞䌂傋幋閘䔇崓庯懫膄㔗套悩埇傖肉启誂毖䔇淉嘩严橬婴婻婉劯䔇淉嘩昄昄扞䌂傋闼幽認麯垂鍙婪橬啕䓉婉劯䔇淉嘩严嘖滇套悩淉嘩昄䌂傋䕩劯闼幽婬婻償庯淉嘩严鄘滇䕩劯䔇淉嘩严㔗埇傖锔誺劉庖锊婻弄滯認底淉嘩严彖彆滇 SORT1, SORT2, LTCMP, GTCMP 锬釹㔗套悩婘弄滯庖 MERGES 䔇劯施剘䩕䘖庖噽婺䔇傂嘘婔婻闼幽係䂘儖准噙䚺䩕䔇劉庖 <, <, <, > 㔗劯湙套悩認啕䓉淉嘩严锬釹婺䔇傂嘘婔婻庺䯄庖闼幽儖啺螆 MERGES 婺锊劆䔇啹溴埇傖埻弄滯噽婺婔鄘彖淉嘩严䇽劯螷係䂘准噙噽垄䔇㔗

啕䓉懫膄淉嘩严䔇淉嘩昄䌂傋埇傖傯埇肉劽誂毖䔇淉嘩严䔇淉嘩昄䌂傋嘐亿庺準啹溴启 COMMUTATOR 婔湙埻驔襕婘認底床埖婺䂍庺淉嘩严劉㔗鍴麂嘹嘪䫘庖䬹垔锬埡䔇淉嘩严劉闼幽喍婔婻 MERGES 䇽劯螷係䂘准噙䂖誗儌轿崘庖㔗启 COMMUTATOR 傖埪 NEGATOR 婔湙套悩嘹䵄噓婘噽垄淉嘩严嬉垔幬庖䕩京淉嘩严闼幽係䂘埇傖彽嘩嚻淉嘩严螄嘘㔗

誻橬婔底凹嘹湺螄婺埇肉劽誂毖䔇淉嘩严䔇鍇媹鍊彽㔗認底鍊彽䕞嬉澇橬赆 CREATE OPERATOR 演昖嘖滇套悩婋麵幋婔婉婺䩘䔇臺肉劽誂毖嚔婘誊臯施崌蘖

㔊濘懟㔏婘婔婻埇肉劽誂毖淉嘩严婋北䔇庘昄媙釂湺螄婺愩幙(immutable)潡蔙䘿垔(stable)㔗套悩垄滇滷崌䔇(volatile)闼幽係䂘儖傯婉嘪䫘垄傸䫘庯肉劽誂毖㔗

㔊濘懟㔏婘 PostgreSQL 7.3 傖嬉䚷喍 MERGES 幽婉庻婘襕喍婔婻埇肉劽誂毖䔇淉嘩严媙釂滯䇞喍庺 SORT1SORT2 㔗劯湙LTCMPGTCMP 锬釹幘婉庻婘認底淉嘩严彖彆喍潊庖䇸傼乕 <>


劯锔饡釕嬉誕
䫘潙垔幬淉嘩严婪婔亓欷匘䘵嚘毖埼