0

I have this query:

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;

First doubt:

Why in the subselect (the inner select) r_name='EUROPE' was set again? I understand that the line p_partkey = ps_partkey give to this subselect the same conditions of the outter select, so there is no need to set it again, or not?

Second doubt:

The line r_name = 'EUROPE' in the inner select make some diferences (that i cant see, but i want to know) in the query. If i count the results of the original query i had 460 entrys. And if i get out the r_name = 'EUROPE' of inner select i got only 144 entrys. This look like crazy, with this condition (and consequently more restrictions) and had more results than when i get out the condition. Why this occurs?

FYI (maybe this help why my mind is blowing with this query):

The original query returns:

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

The query without the inner select condition r_name = 'EUROPA' returns:

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

And last the query without (ps_supplycost = ...) returns:

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

Thanks

4

1 回答 1

0

看起来您正在尝试为 p_size = 15 和 p_type 之类的“%BRASS”之类的任何项目找到最便宜的供应成本...您的 SUBQUERY 没有此限制...因此您的主要查询可能有 100 种产品(通过之类的),但是您的 SUBQUERY 可能有 50,000 个条目,因为您没有将其限制为仅具有相同产品类型的 p_size = 15 的那些供应的相同标准。

您可能需要让您的内部查询还包括要应用 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 美元。但是,您可能有 10 个 BRASS #1 的供应商,价格为 1.50 美元,7 个 BRASS #83 的供应商,价格为 42.00 美元,因此您的最终查询将返回 17 条记录(根据此示例)

于 2013-04-03T03:50:56.597 回答