0

私はこのクエリを持っています:

select
    s_acctbal, s_name, n_name, p_partkey, p_mfgr, 
    s_address, s_phone, s_comment
from 
    part, supplier, partsupp, nation, region
where 
    p_partkey = ps_partkey
    and s_suppkey = ps_suppkey
    and p_size = 15
    and p_type like '%BRASS'
    and s_nationkey = n_nationkey
    and n_regionkey = r_regionkey
    and r_name = 'EUROPE'
    and ps_supplycost = (
        select 
            min(ps_supplycost)
        from 
            partsupp, supplier, nation, region
        where 
            p_partkey = ps_partkey
            and s_suppkey = ps_suppkey
            and s_nationkey = n_nationkey
            and n_regionkey = r_regionkey
            and r_name = 'EUROPE'
    )
order by 
    s_acctbal desc, n_name, s_name, p_partkey;

最初の疑問:

副選択 (内部選択)r_name='EUROPE'で再度設定されたのはなぜですか? p_partkey = ps_partkey行がこのサブ選択に外部選択と同じ条件を与えることを理解しているので、再度設定する必要はありませんか?

2番目の疑い:

内側の選択の行r_name = 'EUROPE'は、クエリでいくつかの違いを生み出します(私は見ることができませんが、知りたいです)。元のクエリの結果を数えると、460 のエントリがありました。そして、内部選択から抜け出すと、 r_name = 'EUROPE'144 エントリしか得られませんでした。これは狂ったように見えますが、この状態 (したがってより多くの制限) があり、状態から抜け出したときよりも多くの結果が得られました。なぜこれが起こるのですか?

参考までに(このクエリで私の心が吹き飛ばされているのは、これが役立つかもしれません):

元のクエリは次を返します。

s_acctbal s_name             n_name         p_partkey p_mfgr         s_address                              s_phone         s_comment                                                                                      
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- ---------------------------------------------------------------------------------------------- 
9938.53   Supplier#000005359 UNITED KINGDOM 185358    Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04               33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros

9937.84   Supplier#000005969 ROMANIA        108438    Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa      29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic 

9936.22   Supplier#000005250 UNITED KINGDOM 249       Manufacturer#4 B3rqp0xbSEim4Mpy2RH J                  33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi

9923.77   Supplier#000002324 GERMANY        29821     Manufacturer#4 y3OD9UywSTOk                           17-779-299-1839 quickly express packages breach quiet pinto beans. requ

9871.22   Supplier#000006373 GERMANY        43868     Manufacturer#5 J8fcXWsTqM                             17-813-485-8637 never silent deposits integrate furiously blit

9870.78   Supplier#000001286 GERMANY        81285     Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH  17-516-924-4574 final theodolites cajole slyly special, 

9870.78   Supplier#000001286 GERMANY        181285    Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH  17-516-924-4574 final theodolites cajole slyly special, 

9852.52   Supplier#000008973 RUSSIA         18972     Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9               32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres

9847.83   Supplier#000008097 RUSSIA         130557    Manufacturer#2 xMe97bpE69NzdwLoX                      32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep

9847.57   Supplier#000006345 FRANCE         86344     Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques

9847.57   Supplier#000006345 FRANCE         173827    Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques

内部選択条件のないクエリは次をr_name = 'EUROPA'返します。

s_acctbal s_name             n_name         p_partkey p_mfgr         s_address                               s_phone         s_comment                                                                             
--------- ------------------ -------------- --------- -------------- --------------------------------------- --------------- ------------------------------------------------------------------------------------- 
9938.53   Supplier#000005359 UNITED KINGDOM 185358    Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04                33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros

9817.10   Supplier#000002352 RUSSIA         124815    Manufacturer#2 4LfoHUZjgjEbAKw TgdKcgOc4D4uCYw         32-551-831-1437 blithely pending packages across the ironic accounts grow slyly after the furiously

9721.95   Supplier#000008757 UNITED KINGDOM 156241    Manufacturer#3 Atg6GnM4dT2                             33-821-407-2995 ironic, even dolphins above the furiously ironic foxes sleep slyly around the caref

9643.55   Supplier#000005148 ROMANIA        107617    Manufacturer#1 kT4ciVFslx9z4s79p Js825                 29-252-617-4850 doggedly even ideas boost furiously against the furiously express

9612.94   Supplier#000003228 ROMANIA        198189    Manufacturer#4 KDdpNKN3cWu7ZSrbdqp7AfSLxx,qWB          29-325-784-8187 carefully pending accounts serve. furiously close deposits boost slyly. q

9571.83   Supplier#000004305 ROMANIA        179270    Manufacturer#2 qNHZ7WmCzygwMPRDO9Ps                    29-973-481-1831 furiously final deposits 

9558.10   Supplier#000003532 UNITED KINGDOM 88515     Manufacturer#4 EOeuiiOn21OVpTlGguufFDFsbN1p0lhpxHp     33-152-301-2164 daring, sly accounts breach about th

9280.27   Supplier#000007194 ROMANIA        47193     Manufacturer#3 zhRUQkBSrFYxIAXTfInj vyGRQjeK           29-318-454-2133 slyly ironic requests despite the unusual ins

9249.35   Supplier#000003973 FRANCE         26466     Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b

9249.35   Supplier#000003973 FRANCE         33972     Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b

9201.47   Supplier#000009690 UNITED KINGDOM 67183     Manufacturer#5 CB BnUTlmi5zdeEl7R7                     33-121-267-9529 blithely unusual accounts integrate slyly. platelets 

9094.57   Supplier#000004582 RUSSIA         39575     Manufacturer#1 WB0XkCSG3r,mnQ n,h9VIxjjr9ARHFvKgMDf    32-587-577-1351 asymptotes above the slyly even requests haggle furiously about the regular accounts

最後に、(ps_supplycost = ...) を使用しないクエリは次を返します。

s_acctbal s_name             n_name         p_partkey p_mfgr         s_address                              s_phone         s_comment                                                                                          
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- -------------------------------------------------------------------------------------------------- 
9984.69   Supplier#000008875 ROMANIA        13872     Manufacturer#3 hRdOqKqyU,sHq                          29-132-904-4395 ironic instructions among the ironic requests boost at the ironic, ironic deposits. quick

9955.05   Supplier#000008810 UNITED KINGDOM 73795     Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A          33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox

9955.05   Supplier#000008810 UNITED KINGDOM 81285     Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A          33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox

9938.53   Supplier#000005359 UNITED KINGDOM 185358    Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04               33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros

9937.84   Supplier#000005969 ROMANIA        108438    Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa      29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic 

9936.22   Supplier#000005250 UNITED KINGDOM 249       Manufacturer#4 B3rqp0xbSEim4Mpy2RH J                  33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi

9923.77   Supplier#000002324 GERMANY        29821     Manufacturer#4 y3OD9UywSTOk                           17-779-299-1839 quickly express packages breach quiet pinto beans. requ

9871.22   Supplier#000006373 GERMANY        43868     Manufacturer#5 J8fcXWsTqM                             17-813-485-8637 never silent deposits integrate furiously blit

9870.78   Supplier#000001286 GERMANY        81285     Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH  17-516-924-4574 final theodolites cajole slyly special, 

9870.78   Supplier#000001286 GERMANY        181285    Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH  17-516-924-4574 final theodolites cajole slyly special, 

9852.52   Supplier#000008973 RUSSIA         18972     Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9               32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres

9847.83   Supplier#000008097 RUSSIA         130557    Manufacturer#2 xMe97bpE69NzdwLoX                      32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep

9847.57   Supplier#000006345 FRANCE         66344     Manufacturer#4 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques

9847.57   Supplier#000006345 FRANCE         86344     Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques

9847.57   Supplier#000006345 FRANCE         173827    Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques

9836.93   Supplier#000007342 RUSSIA         4841      Manufacturer#4 JOlK7C1,7xrEZSSOw                      32-399-414-5385 final accounts haggle. bold accounts are furiously dugouts. furiously silent asymptotes are slyly

ありがとう

4

1 に答える 1

0

p_size = 15 で p_type が '%BRASS' のようなアイテムの最も安価な供給コストを見つけようとしているようです... サブクエリにはこの制限がありません... したがって、プライマリ クエリには 100 個の製品が含まれる可能性があります (など)、ただし、同じ製品タイプで p_size = 15 の供給のみという同じ基準に制限していないため、SUBQUERY には 50,000 のエントリが含まれる可能性があります。

おそらく、内部クエリに MIN() を適用するための p_type と GROUP BY も含め、それを結合の基礎として含める必要があります。あなたが持っている命名規則によって、テーブル/フィールドの関係を最もよく推測してクエリを書き直しました...

また、内部クエリが明示的に alias.field 名参照を使用していることにも注意してください。うまくいけば、外側のクエリの同じフィールド名/値と混同しないようにしてください。私は正確ではないかもしれませんが、私はあなたが探しているものの原則に近いと思います.

select
      s_acctbal, 
      s_name, 
      n_name, 
      p_partkey, 
      p_mfgr, 
      s_address, 
      s_phone, 
      s_comment,
      p_type,
      ps_supplycost
   from 
      part
         JOIN partsupp
            ON p_partkey = ps_partkey 
            JOIN supplier
               ON ps_suppkey = s_suppkey 
               JOIN nation
                  ON s_nationkey = n_nationkey 
                  JOIN region
                     ON n_regionkey = r_regionkey
                     AND r_name = 'EUROPE'

         JOIN ( SELECT 
                      P.p_type,
                      MIN( PS.ps_supplycost ) MinCost
                   from 
                      part P
                         JOIN partsupp PS
                            ON P.p_partkey = PS.ps_partkey
                            JOIN supplier S
                               ON PS.ps_suppkey = S.s_suppkey 
                               JOIN nation N
                                  ON S.s_nationkey = N.n_nationkey 
                                  JOIN region R
                                     ON N.n_regionkey = R.r_regionkey
                                     AND R.r_name = 'EUROPE' 
                   where 
                          P.p_size = 15
                      AND P.p_type like '%BRASS'
                   ) MinPerType
            ON p_type = MinPerType.p_type
            AND ps_supplycost = MinPerType.MinCost        
   where 
          p_size = 15
      AND p_type like '%BRASS'
   order by 
      s_acctbal desc, 
      n_name, 
      s_name, 
      p_partkey;

ご覧のとおり、最低価格を検出するための内部クエリは、各「P_TYPE」値に基づいています。真鍮のアイテム、楽器、または製造、配管などの真鍮の金属を扱う場合、各「タイプ」は劇的な価格差を持つ可能性があり、例を探しています:

BRASS #1  (small thing) that supply cost range $1.50 - $2.15  vs
BRASS #83 (huge item) that supply cost range $42.00 - $48.50

BRASS #83 は、1.50 ドルの値札のコストがかからないため、選択されることはありません。ただし、$1.50 の範囲で BRASS #1 の 10 のサプライヤーがあり、$42.00 の BRASS #83 アイテムのサプライヤーが 7 つある場合、最終的なクエリは 17 のレコードを返します (この例による)。

于 2013-04-03T03:50:56.597 に答える