What kind of indexing should I use for below query? Please note I'm using MySQL and MYISAM storage engine and table contains over 6M rows I need to get result faster. I'm ready to do memory tradeoff for this thanks
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`
I've tried
ALTER TABLE `track_db`.`shipment-profile` ADD KEY track_index(`track-item-number`, `ship-date`, `ursa-code`)
but no use :(
EXPLAIN
# 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'
Possible return rows 25,000 to 30,000.
My key_buffer_size is 2 GB
MySQL version 5.6.29
after doing
ALTER TABLE `track_db`.`shipment-profile` ADD KEY track_index(`ship-date`, `track-item-number`)
EXPLAIN
# 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'
Now using right('ursa-code', 4) not in ('DACA', 'CGPA', 'ZYLA')
but not much improvement
EXPLAIN
# 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 status command
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| | |
答案 0 :(得分:1)
有多个问题......
在提及其他非聚合列时,GROUP BY
一列不合适。我认为你应该专注于解开这个问题。可能您需要多个表来保存数据。
前导通配符不能使用索引 - '%DACA'。
唯一可用的索引是INDEX(ship-date
),但这并不是很有用,因为您发现了(ship-date
,track-item-number
)。