我真的虽然这比它简单。它应该是。我正在使用MySQL 5.1。我有一张300万行的表。把它想象成一个产品表。每个产品都有一个brand_id。
brand_id已编入索引。最简单的查询:
select distinct attribute1 from mytable where brand_id in (4,312,122,82,35,313,123,83,360,170,36,314,124,84,361,171,172,37,315,125,85,362,38,316,126,86,363,173,39,317,127,87,364,174,318,128,365,175,319,129,88,366,176,89,367,177,368,178,369,179,420,230,421,231,422,232,470,233,280,424,471,234,281,425,472,235,282,426,473,236,283,427,474,237,284,428,475,238,10,285,429,476,239,286,477,11,287,478,60,12,100,288,479,61,13,101,289,62,14,102,63,340,150,15,103,64,341,151,16,104,65,342,152,17,105,343,153,18,106,390,66,344,154,19,107,391,67,345,155,108,392,68,346,156,109,393,69,347,157,394,348,158,395,349,159,396,397,400,210,398,401,211,399,402,212,450,260,403,213,451,261,404,214,452,262,405,215,453,263,406,216,454,264,407,217,455,265,408,218,409,456,266,1,219,457,267,2,458,268,3,40,459,269,4,41,5,42,90,6,43,320,130,91,7,321,131,92,8,44,322,132,93,9,370,180,45,323,133,94,371,181,46,324,134,95,372,182,47,325,135,96,373,183,48,326,136,97,374,184,49,327,137,98,375,185,328,138,376,186,329,139,99,377,187,378,188,379,189,430,240,431,241,432,242,433,480,290,243,434,481,291,244,435,482,292,245,436,483,293,246,437,484,294,247,438,485,295,248,20,439,486,296,249,21,487,297,488,300,110,298,70,22,489,301,111,299,71,23,302,112,72,24,113,73,350,160,25,303,114,74,351,161,26,304,75,352,162,27,305,115,76,353,163,28,306,116,354,164,29,307,117,77,355,165,308,118,78,356,166,309,119,79,357,167,358,168,359,169,410,220,411,221,412,222,413,460,270,223,414,461,271,224,415,462,272,225,416,463,273,226,417,464,274,227,418,465,275,228,419,466,276,229,467,277,468,278,50,469,279,51,52,140,53,330,54,331,141,332,142,380,190,55,333,143,381,191,56,334,144,382,192,57,335,145,193,383,58,336,146,194,384,59,337,147,195,385,338,148,196,386,339,149,197,387,200,388,198,201,389,19)
需要4秒钟。我调整了各种MySQL设置,并从InnoDB更改为MyISAM。仍然惊人需要4秒。不可思议的。
所以我想我会在brand_id上进行分区。我想,为什么不试试呢?
按键更改表mytable分区(brand_id);
我遇到了:
ERROR 1503(HY000):PRIMARY KEY必须包含表格分区功能中的所有列
不幸的是,我不知道这意味着什么。我真正想要的是这个查询的速度超过4(4!)秒。我该怎么做才能解决这个问题,为什么MySQL不能执行这个非常简单的任务呢?
答案 0 :(得分:0)
将你的一组id粘贴在临时表中,然后将该表连接到mytable。
CREATE TEMPORARY TABLE brand_ids (brand_id int);
INSERT INTO brand_ids (brand_id)
VALUES (4), (312), (122), ...;
SELECT DISTINCT mytable.attribute1
FROM mytable
JOIN brand_ids
ON mytable.brand_id = brand_ids.brand_id;
答案 1 :(得分:0)
要回答有关分区的问题,表的分区函数必须包含表的主键。因此,除非您将brand_id
作为主键的一部分,否则无法根据该列进行分区。
关于使用大型IN条款表现不佳的更一般性问题,可能需要查看this question