0

我有两个 MySQL 表,分别命名为stockinstockout. 现在我想检查两个表以查找名为 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)

谢谢

4

1 回答 1

0

我已经解决了两个表之间的随机查找以添加索引,哪些唯一列在随机检查中。我已经像下面的代码一样在索引中添加了列,并且立即。我对结果执行时间感到惊讶。该查询花费了 3.30 多分钟,当我添加索引时只需要0.03几秒钟。

CREATE INDEX columnname ON tablename(columnname)
于 2013-06-06T09:58:52.140 回答