优化查询组合子查询

时间:2012-12-03 23:28:07

标签: sql sql-server subquery

我有一个SQL Server数据库,我需要存储未知数量的列,所以我有2个这样的表:

  1. Master,包含以下列:id, data1, data2(固定列)
  2. 表格unknown_fields包含以下列:id_master_record, id_field, value_field
  3. 我这样做的查询:

    SELECT id, data1, data2, 
      (SELECT value_field FROM unknown_fields
        WHERE id_master_record = Master.id AND id_field = 1) AS data3
      (SELECT value_field FROM unknown_fields 
        WHERE id_master_record = Master.id AND id_field = 2) AS data4
      (SELECT value_field FROM unknown_fields 
        WHERE id_master_record = Master.id AND id_field = 3) AS data5
     ... SO for each 
    FROM Master
    

    我想知道是否有更好的方法来进行此查询。

3 个答案:

答案 0 :(得分:4)

你可以试试这个,它可能会稍快一点。最好测试两者。在(id_master_record, id_field)上对第二个表进行聚类可能有助于两个查询。

Select
  m.id,
  m.data1,
  m.data2,
  Max(Case u.id_field When 1 Then u.value_field End) As data3,
  Max(Case u.id_field When 2 Then u.value_field End) As data4,
  Max(Case u.id_field When 3 Then u.value_field End) As data5
From
  xmaster m
    Left Outer Join
  unknown_fields u
    On m.id = u.id_master_record
Group By
  m.id,
  m.data1,
  m.data2;

您也可以尝试使用Pivot

Select
  id,
  data1,
  data2,
  [1] as data3,
  [2] as data4,
  [3] as data5
From (
  Select
    m.id,
    m.data1,
    m.data2,
    id_field,
    value_field
  From
    Master m
      left outer join
    unknown_fields u
      On m.id = u.id_master_record
    ) as s
Pivot (
  max(value_field)
For 
    id_field In ([1], [2], [3])
) As p;

http://sqlfiddle.com/#!6/f9bca/11

答案 1 :(得分:1)

您的初始查询是一种相当好的方式来完成您的需要,而我建议您查看查询执行计划,并尝试在field_id上提高索引,这可能会加快您的查询速度。

答案 2 :(得分:0)

另一种可能的重写:

SELECT Master.id, Master.data1, Master.data2, 
       u3.value_field AS data3,
       u4.value_field AS data4,
       u5.value_field AS data5,
       ... so for each 
FROM Master
  LEFT JOIN unknown_fields AS u3
    ON  u3.id_master_record = Master.id 
    AND u3.id_field = 1
  LEFT JOIN unknown_fields AS u4
    ON  u4.id_master_record = Master.id 
    AND u4.id_field = 2
  LEFT JOIN unknown_fields AS u5
    ON  u5.id_master_record = Master.id 
    AND u5.id_field = 3
  ... so for each  ;

如果要优化性能,则应检查各种重写的执行计划,并在服务器中对数据进行测试。表格上的索引对提高效率至关重要。聚簇索引的选择,尤其是表unknown_fields的选择也会影响此查询。

unknown_fields的两个可能索引是(id_master_record, id_field, value_field)和(不同排序):(id_field, id_master_record, value_field)