正确索引两个mysql表之间的随机查找

时间:2013-05-30 04:58:20

标签: mysql indexing

我有两个名为stockinstockout的MySQL表。现在,我想检查两个表中名为serialno的唯一列,以查找库存手中的行。我已经尝试了4个选择查询行到两个表之间的随机查找。但是,每个查询都需要花费太多时间来执行。你可以检查表的表索引是否有任何错误的问题?如果您有任何更正或建议,我将不胜感激。

mysql> SHOW INDEX FROM stockin\G
*************************** 1. row ***************************
        Table: stockin
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: stockin_id
    Collation: A
  Cardinality: 14657
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 2. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: shiptype
 Seq_in_index: 1
  Column_name: shiptype
    Collation: A
  Cardinality: 18
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 3. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: userid
 Seq_in_index: 1
  Column_name: userid
    Collation: A
  Cardinality: 22
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 4. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: uom
 Seq_in_index: 1
  Column_name: uom
    Collation: A
  Cardinality: 10
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 5. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: warehouseid
 Seq_in_index: 1
  Column_name: warehouse_id
    Collation: A
  Cardinality: 6
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 6. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: project_id_for_stockin
 Seq_in_index: 1
  Column_name: project_id
    Collation: A
  Cardinality: 2
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 7. row ***************************
        Table: stockin
   Non_unique: 1
     Key_name: cus_id
 Seq_in_index: 1
  Column_name: cus_id
    Collation: A
  Cardinality: 2
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
7 rows in set (0.01 sec)

mysql> SHOW INDEX FROM stockout\G
*************************** 1. row ***************************
        Table: stockout
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: stockout_id
    Collation: A
  Cardinality: 19654
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 2. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: eng_id
 Seq_in_index: 1
  Column_name: eng_id
    Collation: A
  Cardinality: 5
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 3. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: project_id
 Seq_in_index: 1
  Column_name: project_id
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 4. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: warehouseid
 Seq_in_index: 1
  Column_name: warehouseid
    Collation: A
  Cardinality: 47
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 5. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: response_type_id
 Seq_in_index: 1
  Column_name: response_type_id
    Collation: A
  Cardinality: 5
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 6. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: cus_id
 Seq_in_index: 1
  Column_name: cus_id
    Collation: A
  Cardinality: 1
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 7. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: employee_id
 Seq_in_index: 1
  Column_name: employee_id
    Collation: A
  Cardinality: 19
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 8. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: userid
 Seq_in_index: 1
  Column_name: userid
    Collation: A
  Cardinality: 1
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 9. row ***************************
        Table: stockout
   Non_unique: 1
     Key_name: uom
 Seq_in_index: 1
  Column_name: uom
    Collation: A
  Cardinality: 11
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
9 rows in set (0.02 sec)

由于

1 个答案:

答案 0 :(得分:0)

我已经解决了两个表之间的随机查找以添加索引,其中唯一列是随机检查的。我在索引中添加了列,如下面的代码,并立即。我对结果执行时间感到惊讶。该查询耗时超过3.30分钟,当我添加索引时只需0.03秒。

CREATE INDEX columnname ON tablename(columnname)