我应该在 MYSQL 和 MYISAM table 引擎中使用什么样的索引

What kind of index should I use in MYSQL with MYISAM table engine

我应该为以下查询使用什么样的索引?请注意我正在使用 MySQL 和 MYISAM 存储引擎,并且 table 包含超过 600 万行我需要更快地获得结果。我准备为此做内存权衡谢谢

    SELECT 
            `track-item-number` AS awb,
            GROUP_CONCAT(DISTINCT(`last-known-location-cd`) ORDER BY last_update SEPARATOR '> ') as PATH,
            `pickup-route-number` AS pickupRoute,
            `service-type` AS service,
            `ursa-code`  AS ursa,
            GROUP_CONCAT(DISTINCT(`shipper-reference-notes`) SEPARATOR '|') as REF,
            MIN(`ship-date`) AS shipDate,
            GROUP_CONCAT(DISTINCT(`delivery-date-time`) SEPARATOR '|') as deliveryDateTime,
            GROUP_CONCAT(DISTINCT(`received-by-name`) SEPARATOR '|') as receivedBy,
            `shipper-account-number` AS shipperAccount,
            `bill-to-account-number` AS billtoAccount,
            `weight-uom` AS weightType,
            `shipment-weight` AS weight,
            `shipment-weight-uom` AS shipWeightType,
            `dim-source` AS dimSource,
            `package-weight-source` AS pkgWeightSource,
            `svc-commit-date` AS serviceCommitDate,
            `svc-commit-time` AS serviceCommiteTime,
            `pkg-length` AS pkgLength,
            `pkg-width` AS pkgWidth,
            `pkg-height` AS pkgHeight,
            `tracking-item-form-cd` AS trackingItemCode,
            `dim-volume` AS dimVolume,
            `doc-nondoc-flag` AS docFlag,
            `service-area` AS serviceArea,
            `tracking-item-unique-id` AS trackingUniqueId,
            `packaging-type` AS pkgType,
            `ese-generation-timestamp` AS eseTimestamp,
            `shipment-pkg-number` AS pkgNumber,
            `shipment-pkg-count` AS pkgCount,
            `package-weight` pkgWeight,
            `carrier-oid` AS carrierOid,
            `svc-commit-timestamp` AS serviceCommitTimestamp,
            `operational-scan-count` AS opScanCount,
            `dim-volume-uom` AS dimWeightType,
            `revenue-system-routing-cd` AS revSystemRouteCode,
            `revenue-dt` AS revDate,
            `unique_id`,
            `last_update`,
            `id`,
            `cosmos-dspch-nbr` AS dispatchNbr,
            `cer-id` AS cerId,
            `lpar-state` AS lparState,
            `lpar-disposition-state-cd` AS lparStateCode,
            `return-type-cd` AS returnTypeCode,
            `return-reason-group` AS returnReasonGroup,
            `recipient-account-number` AS recipAccountNumber,
            `shipper-tax-id-nbr` AS shipperTaxId
    FROM 
            `track_db`.`shipment-profile`
    WHERE
            `ship-date`>20160201 and
            `ursa-code` not like '%DACA' AND
            `ursa-code` not like '%CGPA' AND
            `ursa-code` not like '%ZYLA' 
    GROUP BY
            `track-item-number`

我试过了

    ALTER TABLE `track_db`.`shipment-profile` ADD KEY track_index(`track-item-number`, `ship-date`, `ursa-code`)  

但没用:(

解释

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
 '1', 'SIMPLE', 'shipment-profile', 'ALL', 'track_index', NULL, NULL, NULL, '4609925', 'Using where; Using filesort'

可能 return 行 25,000 到 30,000。

我的 key_buffer_size 是 2 GB

MySQL 版本 5.6.29

完成后

  ALTER TABLE `track_db`.`shipment-profile` ADD KEY track_index(`ship-date`, `track-item-number`)

解释

   # id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
    '1', 'SIMPLE', 'shipment-profile', 'ALL', 'track_index', NULL, NULL, NULL, '4610387', 'Using where; Using filesort'

现在使用 right('ursa-code', 4) not in ('DACA', 'CGPA', 'ZYLA') 但没有太大改进 解释

 # id, select_type, table,            type, possible_keys, key, key_len, ref,    rows,      Extra
 '1', 'SIMPLE',  'shipment-profile', 'ALL', 'track_index', NULL, NULL,   NULL, '4661631', 'Using where; Using filesort'

Table状态命令

  table_name      |Engine|Version|Row_format|table_rows|Avg_row_length|Data_length|Max_data_length|Index_length|Data_free|Auto_increment|    Create_time    |    Update_time    |     Check_time    | table_collation|Checksum|Create_options|table_comment
  shipment-profile|MyISAM|   10  |  Dynamic | 4738574  |      727     | 2719772024|281474976710655|140731392   |    0    |    4738575   |2016-03-02 17:20:02|2016-03-05 10:55:40|2016-03-02 17:25:20| utf8_general_ci|        |              |

存在多个问题...

不是'proper'到GROUP BY一列同时提到了其他非聚合列。我认为你应该集中精力解决这个问题。可能您需要不止一个 table 保存数据。

前导通配符不能使用索引 -- '%DACA'。

唯一可用的索引是 INDEX(ship-date),但它不是很有用,正如您通过 (ship-date, track-item-number).[=14 发现的那样=]