如何从多列主键中选择一系列行?

时间:2013-12-30 16:34:53

标签: mysql sql database

我正在尝试通过MySQL 5.5中的行进行分块并且为此我想要选择两个主键之间的范围(我可以轻松获得)。当主键只有一列时,这是微不足道的。但是,我需要分块的一些表在主键中有多列,而我还没有弄清楚如何在一个预备语句中使用它。

这是一个包含一些数据的示例表:

CREATE TABLE test (
  a INT UNSIGNED NOT NULL,
  b INT UNSIGNED NOT NULL,
  c INT UNSIGNED NOT NULL,
  d VARCHAR(255) DEFAULT '', -- various data columns
  PRIMARY KEY (a, b, c)
) ENGINE=InnoDB;

INSERT INTO test VALUES 
(1, 1, 1),
(1, 1, 2),
(1, 1, 3),
(1, 2, 1),
(1, 2, 2),
(1, 2, 3),
(1, 3, 1),
(1, 3, 3),
(2, 1, 1),
(2, 1, 2),
(2, 2, 2),
(2, 3, 1),
(2, 3, 3),
(3, 1, 2),
(3, 1, 3),
(3, 2, 1),
(3, 2, 2),
(3, 2, 3),
(3, 3, 1),
(3, 3, 3);

如果我有两个主键,如(1,1,3)和(3,2,1),则以下语句可行。 a1,b1和c1是第一个主键的值,a2,b2和c2是第二个主键的值:

SELECT * FROM test WHERE a = a1 AND b = b1 AND c >= c1
UNION
SELECT * FROM test WHERE a = a1 AND b > b1
UNION
SELECT * FROM test WHERE a > a1 AND a < a2
UNION
SELECT * FROM test WHERE a = a2 AND b < b2
UNION
SELECT * FROM test WHERE a = a2 AND b = b2 AND c <= c2

或者

SELECT * FROM test WHERE a = 1 AND b = 1 AND c >= 3
UNION
SELECT * FROM test WHERE a = 1 AND b > 1
UNION
SELECT * FROM test WHERE a > 1 AND a < 3
UNION
SELECT * FROM test WHERE a = 3 AND b < 2
UNION
SELECT * FROM test WHERE a = 3 AND b = 2 AND c <= 1

哪个给出了

(1, 1, 3),
(1, 2, 1),
(1, 2, 2),
(1, 2, 3),
(1, 3, 1),
(1, 3, 3),
(2, 1, 1),
(2, 1, 2),
(2, 2, 2),
(2, 3, 1),
(2, 3, 3),
(3, 1, 2),
(3, 1, 3),
(3, 2, 1)

但是当第一列相同时,例如,上述操作失败,例如(1,2,2)和(1,3,1)。在这种情况下,第二个和第四个SELECT选择太多。

SELECT * FROM test WHERE a = 1 AND b = 2 AND c >= 2
UNION
SELECT * FROM test WHERE a = 1 AND b > 2
UNION
SELECT * FROM test WHERE a > 1 AND a < 1
UNION
SELECT * FROM test WHERE a = 1 AND b < 3
UNION
SELECT * FROM test WHERE a = 1 AND b = 3 AND c <= 1

哪个给出了

(1, 1, 1), -- erroneously selected from: SELECT * FROM test WHERE a = 1 AND b < 3
(1, 1, 2), -- erroneously selected from: SELECT * FROM test WHERE a = 1 AND b < 3
(1, 1, 3), -- erroneously selected from: SELECT * FROM test WHERE a = 1 AND b < 3
(1, 2, 1), -- erroneously selected from: SELECT * FROM test WHERE a = 1 AND b < 3
(1, 2, 2),
(1, 2, 3),
(1, 3, 1),
(1, 3, 3)  -- erroneously selected from: SELECT * FROM test WHERE a = 1 AND b > 2

所需的输出是

(1, 2, 2),
(1, 2, 3),
(1, 3, 1)

我想要一个适用于所有主键范围的语句,包括第一列和第二列的相同值。我还在主键中有4列的表,在这种情况下我会扩展模式。

我希望每个表只有一个语句,而不是动态创建查询,因为查询将在表格中执行时执行多达一百万次。有些表有超过100M的行。

我宁愿避免构造多个语句,因为我有数百个要按照这种模式编写,而编写更多将会更多的工作。如果这是唯一的选择,我会这样做。

我目前使用参数化查询,并从两个主键以编程方式生成值,在应用程序层中处理所需的重复值(上例中的a1 x3,b1 x2,a2 x3,b2 x2)。因此,传递参数的重复值对我来说很简单。

我在这一点上的最佳猜测是重复SELECT,并使用WHERE子句的其他部分比较主键列的值。

1 个答案:

答案 0 :(得分:2)

我会使用此查询来选择范围:

SELECT * 
FROM test
WHERE (a,b,c) >= (1, 1, 3) 
  and (a,b,c) <= (3, 2, 1)

演示:http://www.sqlfiddle.com/#!2/d6cf7b/4


不幸的是,MySql无法对上述查询执行范围优化,请参阅以下链接:http://dev.mysql.com/doc/refman/5.7/en/range-optimization.html#range-access-single-part
(章:8.2.1.3.4。行构造函数表达式的范围优化)
他们说从verion 5.7开始,MySql只能优化表单的查询:

WHERE ( col_1, col_2 ) IN (( 'a', 'b' ), ( 'c', 'd' ));



基本上上面的查询等同于这个:

SELECT * 
FROM test
WHERE  
     a = 1 and b = 1 and c >= 3 -- lowest end
     or 
     a = 3 and b = 2 and c <= 1 -- highest end
     or 
     a = 1 and b > 1
     or
     a = 3 and b < 2
     or 
     a  > 1 and a < 3
;

MySql可能会对此查询形式使用范围访问方法优化,请参阅下面的链接
 (章:8.2.1.3.2。多部分索引的范围访问方法):
http://dev.mysql.com/doc/refman/5.7/en/range-optimization.html