表结构
CREATE TABLE orders
(
id
bigint(20) unsigned NOT NULL ,
account_id
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
product_id
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
product_id_org
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
loan
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
loan_org
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
float_rate
int(11) NOT NULL DEFAULT ‘0’ ,
float_rate_org
int(11) NOT NULL DEFAULT ‘0’ ,
period
int(10) unsigned NOT NULL DEFAULT ‘0’ ,
period_org
int(10) unsigned NOT NULL DEFAULT ‘0’ ,
pre_order
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
roll_times
int(10) NOT NULL DEFAULT ‘0’ ,
min_repay_amount
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
op_uid
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
risk_ctl_status
smallint(2) unsigned NOT NULL DEFAULT ‘0’ ,
risk_ctl_finish_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
risk_ctl_regular
varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘’ ,
reject_reason
smallint(2) unsigned NOT NULL DEFAULT ‘0’ ,
is_temporary
tinyint(1) unsigned NOT NULL DEFAULT ‘0’,
is_overdue
tinyint(2) NOT NULL DEFAULT ‘0’ ,
is_dead_debt
tinyint(1) unsigned NOT NULL DEFAULT ‘0’ ,
is_reloan
tinyint(1) unsigned NOT NULL DEFAULT ‘0’ ,
is_up_hold_photo
tinyint(1) NOT NULL DEFAULT ‘0’ ,
check_status
tinyint(1) unsigned NOT NULL DEFAULT ‘1’ ,
apply_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
check_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
phone_verify_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’,
repay_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
loan_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
finish_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
penalty_utime
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
credit_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
random_value
tinyint(3) unsigned NOT NULL DEFAULT ‘0’ ,
fixed_random
int(10) NOT NULL DEFAULT ‘0’ ,
livingbest_reloanhand_similar
varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘’ ,
after_black_similar
varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘’ ,
random_mark
int(10) NOT NULL DEFAULT ‘0’,
op_desc
varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘’ ,
from_platform
tinyint(2) NOT NULL DEFAULT ‘0’ ,
stage_num
int(20) unsigned NOT NULL DEFAULT ‘0’ ,
stage_info
text COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
third_paid_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
amount
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
overdue_run_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
risk_hyrule_status
int(10) DEFAULT ‘0’ ,
order_invalid_type
tinyint(2) unsigned NOT NULL DEFAULT ‘0’ ,
order_invalid_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’,
first_diff_tag
int(11) unsigned NOT NULL DEFAULT ‘0’ ,
phone_verify_failed_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’,
phone_verify_failed_type
tinyint(2) unsigned NOT NULL DEFAULT ,
confirm_enter_time
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
loan_flow_version
tinyint(5) unsigned NOT NULL DEFAULT ‘0’ ,
confirm_disburse_time
bigint(20) NOT NULL DEFAULT ‘0’ ,
offline_handle_time
bigint(20) NOT NULL DEFAULT ‘0’ ,
is_jump_confirm
smallint(2) NOT NULL DEFAULT ‘0’ ,
is_deleted
tinyint(2) NOT NULL DEFAULT ‘0’ ,
orders_ctime
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
orders_utime
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
orders_ext_ctime
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
orders_ext_utime
bigint(20) unsigned NOT NULL DEFAULT ‘0’ ,
PRIMARY KEY (id
) /*T![clustered_index] CLUSTERED */,
KEY idx_orders_account_id
(account_id
),
KEY idx_orders_apply_time
(apply_time
),
KEY idx_orders_orders_ctime
(orders_ctime
),
KEY idx_orders_utime
(orders_utime
),
KEY idx_orders_ext_utime
(orders_ext_utime
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT=‘订单表’
有问题的查询语句执行计划:
±-----------------------------------±--------±----------±------------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
±-----------------------------------±--------±----------±------------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| HashAgg_8 | 1.00 | root | | funcs:min(Column#65)->Column#62, funcs:max(Column#66)->Column#63 |
| └─Projection_36 | 45.21 | root | | if(and(and(gt(a.orders.apply_time, 0), gt(a.orders.stage_num, 1)), le(datediff(2023-05-10 17:45:34, from_unixtime(plus(div(cast(a.orders.apply_time, decimal(20,0) UNSIGNED BINARY), 1000), 18000))), 30)), cast(a.orders.loan, decimal(20,0) UNSIGNED BINARY), )->Column#65, if(and(and(gt(a.orders.apply_time, 0), gt(a.orders.stage_num, 1)), le(datediff(2023-05-10 17:45:34, from_unixtime(plus(div(cast(a.orders.apply_time, decimal(20,0) UNSIGNED BINARY), 1000), 18000))), 720)), cast(a.orders.loan, decimal(20,0) UNSIGNED BINARY), )->Column#66 |
| └─IndexLookUp_18 | 45.21 | root | | |
| ├─Selection_17(Build) | 45.21 | cop[tikv] | | ne(a.orders.id, 1) |
| │ └─IndexRangeScan_15 | 45.21 | cop[tikv] | table:orders, index:idx_orders_account_id(account_id) | range:[210802010000721168,210802010000721168], keep order:false |
| └─TableRowIDScan_16(Probe) | 45.21 | cop[tikv] | table:orders | keep order:false |
±-----------------------------------±--------±----------±------------------------------------------------------±------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
使用with改写后:
±-----------------------------------±--------±----------±------------------------------------------------------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
±-----------------------------------±--------±----------±------------------------------------------------------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| HashAgg_16 | 1.00 | root | | funcs:min(Column#68)->Column#66, funcs:max(Column#69)->Column#67 |
| └─Projection_20 | 45.21 | root | | if(and(and(gt(a.orders.apply_time, 0), gt(a.orders.stage_num, 1)), le(Column#65, 30)), a.orders.loan, )->Column#68, if(and(and(gt(a.orders.apply_time, 0), gt(a.orders.stage_num, 1)), le(Column#65, 720)), a.orders.loan, )->Column#69 |
| └─CTEFullScan_18 | 45.21 | root | CTE:tmp | data:CTE_0 |
| CTE_0 | 45.21 | root | | Non-Recursive CTE |
| └─Projection_7(Seed Part) | 45.21 | root | | a.orders.loan, a.orders.stage_num, a.orders.apply_time, datediff(2023-05-10 17:46:47, from_unixtime(plus(div(cast(a.orders.apply_time, decimal(20,0) UNSIGNED BINARY), 1000), 18000)))->Column#61 |
| └─IndexLookUp_14 | 45.21 | root | | |
| ├─Selection_13(Build) | 45.21 | cop[tikv] | | ne(a.orders.id, 1) |
| │ └─IndexRangeScan_11 | 45.21 | cop[tikv] | table:orders, index:idx_orders_account_id(account_id) | range:[210802010000721168,210802010000721168], keep order:false |
| └─TableRowIDScan_12(Probe) | 45.21 | cop[tikv] | table:orders | keep order:false |
±-----------------------------------±--------±----------±------------------------------------------------------±--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set (0.01 sec)