分析以下sql的问题 SELECT COUNT(1) FROM xb_base_user AS base_user INNER JOIN (SELECT contract_account.cert_no FROM xb_sign_contract_account AS contract_account LEFT JOIN xb_sign_contract AS sign_contract ON sign_contract.id = contract_account.contract_id WHERE contract_account.account_type = 0 AND sign_contract.resource IN ("YJB", "GD", "KHHC") AND sign_contract.project_id IN (1747, 1793, 1877, 1878, 1879, 1929, 1943, 1979, 1983, 1984, 1989, 2142, 2246) GROUP BY contract_account.cert_no) AS tmep ON tmep.cert_no = base_user.card_no WHERE base_user.cert_auth = 1;
时间: 2024-02-14 10:28:43 浏览: 81
这个SQL查询的目的是计算符合条件的用户数量。它使用了多个表和子查询来进行连接和过滤。
首先,从 `xb_sign_contract_account` 表和 `xb_sign_contract` 表中获取符合条件的 `cert_no`。条件包括 `account_type` 等于 0,`resource` 在 ["YJB", "GD", "KHHC"] 中,以及 `project_id` 在给定的一组值中。
然后,这些符合条件的 `cert_no` 被分组,并与 `xb_base_user` 表中的 `card_no` 进行连接,通过 `tmep.cert_no = base_user.card_no` 条件进行匹配。
最后,在连接后的结果上,通过 `base_user.cert_auth = 1` 条件过滤,计算满足条件的记录数。
请注意,这只是一个SQL查询的分析,具体执行效率还需根据数据量、索引情况和数据库优化等因素进行评估。
相关问题
优化SQL语句SELECT * FROM ( SELECT d.driver_id, d.driver_name, v.numberplate, v.vehicle_type, d.driver_photo, d.phone_number, ( SELECT count( 1 ) FROM waybill_info WHERE transport_status<>9 and driver_id = d.driver_id ) AS transportStatus FROM driver_info AS d INNER JOIN driver_vehicle_bind AS d1 ON d.driver_id = d1.driver_id INNER JOIN vehicle_info AS v ON d1.vehicle_id = v.vehicle_id WHERE d1.is_use = 1 AND d1.bind_status = 1 AND v.cert_status = 1 AND d.cert_status = 2 ) AS s WHERE s.driver_id NOT IN ( SELECT driver_id FROM collect_blacklist WHERE user_id = #{userId})
可以尝试对该SQL语句进行以下优化:
1. 减少不必要的列查询:在SELECT语句中只选择需要的列,而不是使用通配符*,可以减少数据传输量,提高查询效率。
2. 使用JOIN代替子查询:在内部查询中使用JOIN语句代替子查询,可以提高查询效率。因为JOIN语句可以将多个表的数据一次性查询出来,而子查询会多次查询。
3. 使用EXISTS代替IN:在外部查询中使用EXISTS代替IN,可以提高查询效率。因为EXISTS只需要判断是否存在符合条件的记录,而IN需要先查询出符合条件的所有记录。
下面是优化后的SQL语句:
SELECT d.driver_id, d.driver_name, v.numberplate, v.vehicle_type, d.driver_photo, d.phone_number, count(w.waybill_id) AS transportStatus
FROM driver_info d
INNER JOIN driver_vehicle_bind d1 ON d.driver_id = d1.driver_id
INNER JOIN vehicle_info v ON d1.vehicle_id = v.vehicle_id
LEFT JOIN waybill_info w ON w.driver_id = d.driver_id AND w.transport_status <> 9
WHERE d1.is_use = 1
AND d1.bind_status = 1
AND v.cert_status = 1
AND d.cert_status = 2
AND NOT EXISTS (
SELECT 1
FROM collect_blacklist
WHERE driver_id = d.driver_id
AND user_id = #{userId}
)
GROUP BY d.driver_id, d.driver_name, v.numberplate, v.vehicle_type, d.driver_photo, d.phone_number
ORDER BY d.driver_id;
注意,优化SQL时需要根据具体情况进行选择和应用,以上仅供参考。
insert overwrite table discountdw.dwd_sd_adds_order_bill_inc partition(dt = '2023-06-06') select t1.order_bill_id, t1.counterfoil_no, t1.acceptor, date_format(to_utc_timestamp(cast(t1.expiry_date as bigint) ,'GMT-8'),'YYYY-MM-dd'), t2.company_id, t1.cert_no, t1.company_name, t1.third_order_id, t1.counterfoil_amt/10000, t1.transaction_amt/10000, t1.rate, '3bp' as service_tariffing, ((DATEDIFF(to_utc_timestamp(t1.expiry_date ,'GMT-8'),to_utc_timestamp(t1.transaction_date ,'GMT-8') ) + adjust_days)* 0.0003 *(counterfoil_amt))/ 360 as service_fee, 360 as total_days, DATEDIFF(to_utc_timestamp(t1.expiry_date ,'GMT-8'),to_utc_timestamp(t1.transaction_date ,'GMT-8') ) + adjust_days as modulation_date, t3.channel_type, t3.bank_name, date_format(to_utc_timestamp(cast(t1.transaction_date as bigint) ,'GMT-8'),'YYYY-MM-dd'), t1.order_status_code, t1.order_status_msg, t4.fee_amt, t4.status, t1.tenant_id, t5.revenue, to_utc_timestamp(cast(t1.create_date as bigint) ,'GMT-8'), to_utc_timestamp(cast(t1.update_date as bigint) ,'GMT-8') from (select * from discountdw.ods_adds_order_bill_inc where dt ='2023-06-06' and channel_id=101110004 )t1 left join (select * from mecdw.ods_company_full where platform_id='sdpjw')t2 on t1.cert_no=t2.cert_no and t1.tenant_id=t2.tenant_id left join discountdw.dim_adds_product_full t3 on t1.partner_id=t3.partner_id and t1.product_id=t3.product_id left join (select * from mecdw.dwd_sc_fee_record_full where dt='2023-06-06' and biz_type=2 ) t4 on t1.order_bill_id=t4.third_id left join (select * from discountdw.ods_sd_order_ext_inc where dt='2023-06-06') t5 on t1.order_bill_id=t5.order_bill_id left join sdpjwdw.dim_holiday_info_full t6 on date_format(to_utc_timestamp(t1.expiry_date ,'GMT-8'),'YYYY-MM-dd') = t6.civic_holiday ;
这是一条 SQL 语句,看起来比较长。以下是一些可以考虑优化的建议:
1. 将表名、字段名等缩短,以减少字符数和提高可读性。
2. 将复杂的表连接操作拆分成多个子查询,以降低查询复杂度。
3. 对于一些计算操作,可以考虑使用内置函数或自定义函数,以简化代码并提高可维护性。
4. 对于一些重复的操作,可以将其提取成变量或常量,以减少重复代码。
综上所述,优化后的 SQL 可能类似于以下代码(仅供参考):
```
-- 定义常量
SET adjust_days = 1;
-- 查询订单表
WITH order_data AS (
SELECT
o.order_bill_id,
o.counterfoil_no,
o.acceptor,
DATE_FORMAT(TO_UTC_TIMESTAMP(CAST(o.expiry_date AS BIGINT), 'GMT-8'), 'YYYY-MM-dd') AS expiry_date,
o.cert_no,
o.company_name,
o.third_order_id,
o.counterfoil_amt / 10000 AS counterfoil_amt,
o.transaction_amt / 10000 AS transaction_amt,
o.rate,
'3bp' AS service_tariffing,
((DATEDIFF(TO_UTC_TIMESTAMP(o.expiry_date, 'GMT-8'), TO_UTC_TIMESTAMP(o.transaction_date, 'GMT-8')) + adjust_days) * 0.0003 * (o.counterfoil_amt)) / 360 AS service_fee,
360 AS total_days,
DATEDIFF(TO_UTC_TIMESTAMP(o.expiry_date, 'GMT-8'), TO_UTC_TIMESTAMP(o.transaction_date, 'GMT-8')) + adjust_days AS modulation_date,
o.order_status_code,
o.order_status_msg,
o.tenant_id,
TO_UTC_TIMESTAMP(CAST(o.create_date AS BIGINT), 'GMT-8') AS create_date,
TO_UTC_TIMESTAMP(CAST(o.update_date AS BIGINT), 'GMT-8') AS update_date
FROM discountdw.ods_adds_order_bill_inc o
WHERE o.dt = '2023-06-06' AND o.channel_id = 101110004
),
-- 查询公司表
company_data AS (
SELECT
c.company_id,
c.cert_no,
c.tenant_id
FROM mecdw.ods_company_full c
WHERE c.platform_id = 'sdpjw'
),
-- 查询产品表
product_data AS (
SELECT
p.partner_id,
p.product_id,
p.channel_type,
p.bank_name
FROM discountdw.dim_adds_product_full p
),
-- 查询费用记录表
fee_data AS (
SELECT
f.third_id,
f.fee_amt,
f.status
FROM mecdw.dwd_sc_fee_record_full f
WHERE f.dt = '2023-06-06' AND f.biz_type = 2
),
-- 查询订单扩展信息表
order_ext_data AS (
SELECT
e.order_bill_id,
e.revenue
FROM discountdw.ods_sd_order_ext_inc e
WHERE e.dt = '2023-06-06'
),
-- 查询节假日表
holiday_data AS (
SELECT
h.civic_holiday
FROM sdpjwdw.dim_holiday_info_full h
)
-- 合并数据
INSERT OVERWRITE TABLE discountdw.dwd_sd_adds_order_bill_inc PARTITION(dt = '2023-06-06')
SELECT
o.order_bill_id,
o.counterfoil_no,
o.acceptor,
o.expiry_date,
c.company_id,
o.cert_no,
o.company_name,
o.third_order_id,
o.counterfoil_amt,
o.transaction_amt,
o.rate,
o.service_tariffing,
o.service_fee,
o.total_days,
o.modulation_date,
p.channel_type,
p.bank_name,
DATE_FORMAT(TO_UTC_TIMESTAMP(CAST(o.transaction_date AS BIGINT), 'GMT-8'), 'YYYY-MM-dd'),
o.order_status_code,
o.order_status_msg,
f.fee_amt,
f.status,
o.tenant_id,
e.revenue,
o.create_date,
o.update_date
FROM order_data o
LEFT JOIN company_data c ON o.cert_no = c.cert_no AND o.tenant_id = c.tenant_id
LEFT JOIN product_data p ON o.partner_id = p.partner_id AND o.product_id = p.product_id
LEFT JOIN fee_data f ON o.order_bill_id = f.third_id
LEFT JOIN order_ext_data e ON o.order_bill_id = e.order_bill_id
LEFT JOIN holiday_data h ON o.expiry_date = DATE_FORMAT(TO_UTC_TIMESTAMP(h.civic_holiday, 'GMT-8'), 'YYYY-MM-dd');
```
阅读全文