本帖最后由 innerDBA 于 2023-6-8 16:48 编辑
我有个两表关联的SQL, 一个业务表,一个字典表, 然后 ORDER BY REQUES_DATE_TIME DESC LIMIT 0,10; 执行了5秒, 如果使用PAY_TIME 索引执行245毫秒
使用OPTIMER 去分析为什么? 使用REQEST_DATE_TIME 索引
上面显示执行计划选择走PAY_TIME索引
行评估:
也是PAY_TIME索引优先.
附件条件影响下使用REQUEST_DATETIME_IDX
最终执行计划
我猜测 优化器认为 通过PAY_TIME返回符合条件的行,要进行排序成本,高于通过REQUEST_DATE_TIME 索引消除FILE SORT操作!
不过排序成本 两个索引OPTIMER 并没有去分析跟踪出来!
下面是JSON格式的执行计划:
- {
- "query_block": {
- "select_id": 1,
- "cost_info": {
- "query_cost": "88932.23"
- },
- "ordering_operation": {
- "using_filesort": false,
- "nested_loop": [
- {
- "table": {
- "table_name": "a",
- "partitions": [
- "request_date_time_20221026",
- "request_date_time_20230426",
- "request_date_time_20230503",
- "request_date_time_20230510",
- "request_date_time_20230517",
- "request_date_time_20230524"
- ],
- "access_type": "range",
- "possible_keys": [
- "request_date_time_idx",
- "merchant_trans_no_idx",
- "channel_code_product_code_sktrans_idx",
- "pay_time_sktrans_idx"
- ],
- "key": "request_date_time_idx",
- "used_key_parts": [
- "request_date_time"
- ],
- "key_length": "5",
- "rows_examined_per_scan": 638300,
- "rows_produced_per_join": 17,
- "filtered": "0.00",
- "index_condition": "((`wayangpay`.`a`.`request_date_time` >= '2023-04-19 00:00:00') and (`wayangpay`.`a`.`request_date_time` <= '2023-05-19 23:59:59'))",
- "cost_info": {
- "read_cost": "88936.69",
- "eval_cost": "3.52",
- "prefix_cost": "88940.21",
- "data_read_per_join": "298K"
- },
- "used_columns": [
- "id",
- "channel_code",
- "channel_product_code",
- "amount",
- "request_date_time",
- "customer_name",
- "email",
- "mobile_phone",
- "collect_code",
- "settle_no",
- "status",
- "remit_no",
- "trans_fee_rate",
- "total_trans_fee",
- "trans_fee",
- "platform_trans_fee_rate",
- "platform_trans_fee",
- "merchant_id",
- "merchant_no",
- "trans_no",
- "cp_refund_no",
- "merchant_trans_no",
- "merchant_refund_no",
- "channel_trans_no",
- "merchant_notify_url",
- "notifty_status",
- "extra_content",
- "version",
- "pay_time",
- "create_time",
- "last_update_time",
- "invalidate_time",
- "va_code",
- "va_code_expired_time",
- "goods_info",
- "source_from",
- "pay_back_name",
- "id_card",
- "account_name",
- "bank_name",
- "branch_bank_name",
- "cit_name",
- "location_code",
- "initial_amount"
- ],
- "attached_condition": "((`wayangpay`.`a`.`channel_code` = '153') and (`wayangpay`.`a`.`location_code` = 'ID') and (`wayangpay`.`a`.`status` = '02') and (`wayangpay`.`a`.`channel_product_code` = 'DANAWALLET') and (`wayangpay`.`a`.`pay_time` >= '2023-05-19 00:00:00') and (`wayangpay`.`a`.`pay_time` <= '2023-05-19 23:59:59'))"
- }
- },
- {
- "table": {
- "table_name": "c",
- "access_type": "ref",
- "possible_keys": [
- "INX_M_SMI"
- ],
- "key": "INX_M_SMI",
- "used_key_parts": [
- "merchant_no"
- ],
- "key_length": "123",
- "ref": [
- "wayangpay.a.merchant_no"
- ],
- "rows_examined_per_scan": 1,
- "rows_produced_per_join": 7,
- "filtered": "40.00",
- "cost_info": {
- "read_cost": "17.59",
- "eval_cost": "1.41",
- "prefix_cost": "88961.31",
- "data_read_per_join": "168K"
- },
- "used_columns": [
- "id",
- "merchant_no",
- "register_name",
- "short_name",
- "salesman_no",
- "merchant_type",
- "subject_name"
- ],
- "attached_condition": "(`wayangpay`.`c`.`subject_name` in ('AESYGO','KOALA FIRST','TYGATEWAY','PAYOK'))"
- }
- }
- ]
- }
- }
- }
复制代码
|