注册

【急】使用left join查询变慢

DM_106068 2023/06/20 1066 5 已解决

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:
【操作系统】:
【CPU】:
【问题描述】*:使用left join关联表,查询速度变慢。使用时查询消耗3.6s左右,不使用时查询消耗0.019s左右,相差189.5倍左右,请大佬帮忙分析下怎么优化吧,谢谢!
1.使用left join时sql:

select
	t.id,
	t.invoicetypename_chs,
	t.invoicetypename_cht,
	t.invoicetypename_en,
	t.invoicetypename_es,
	t.invoicetypename_pt,
	t.invoicetypeid,
	t.invoicetypecode,
	t.taxorgid,
	t.invoicecode,
	t.invoicenumber,
	t.invoicedate,
	t.sourcetype,
	t.amount,
	t.taxamount,
	t.amountandtax,
	t.sellertaxpayername,
	t.sellertaxpayerid,
	t.selectconfirmstate,
	t.invveristate,
	t.isdeduct,
	t.istaxreimbursement,
	t.taxreimbursementamount,
	t.administrateid,
	t.candeduction,
	t.note,
	t.selecttype,
	t.selectedtime,
	t.selectoperatestate,
	t.selector,
	t.notdeductionreason,
	t.taxperiod,
	t.transfertax,
	t.taxorgname,
	t.validtax,
	t.namefortax,
	t.applytransferprice,
	t.signstate,
	t.signperson,
	t.signtime,
	t.signtype,
	t.invoicestate,
	t.transferprice,
	t.expandone,
	t.expandtwo,
	t.expandthree,
	t.expandfour,
	t.expandfive,
	t.updateinfo_createdon,
	t.updateinfo_createdby,
	t.updateinfo_lastchangedby,
	t.updateinfo_lastchangedon,
	t.lastchangedby,
	t.lastchangedon,
	t.vouchernumber,
	t.voucherid,
	t.voucherdate,
	t.voucheryear,
	t.doccode,
	t.docid,
	t.doctypecode,
	t.doctypename,
	t.isnewinvoice,
	t.systemcode,
	t.systemname,
	t.fidepartment,
	t.fiunit,
	t.partner,
	t.projectid,
	t.contractid
from
	(
	select
		a.id, g.name_chs as invoicetypename_chs, g.name_cht as invoicetypename_cht, g.name_en as invoicetypename_en, g.name_es as invoicetypename_es, g.name_pt as invoicetypename_pt, g.id as invoicetypeid, g.code as invoicetypecode, a.taxorgid, a.invoiceinfo_invoicecode as invoicecode, a.invoiceinfo_invoicenumber as invoicenumber, a.invoiceinfo_invoicedate as invoicedate, a.sourcetype as sourcetype, a.invoiceinfo_amount as amount, a.invoiceinfo_taxamount as taxamount, a.invoiceinfo_amountandtax as amountandtax, a.seller_taxpayername as sellertaxpayername, a.seller_taxpayerid as sellertaxpayerid,
		case
			when(b.selectconfirmstate is null) then '0'
			else b.selectconfirmstate
		end as selectconfirmstate, a.invveristate, a.isdeduct, a.istaxreimbursement, a.taxreimbursementamount, a.administrateid, b.candeduction, a.note, b.selecttype, b.selectedtime
		, case when(b.selectoperatestate is null) then '0'
		else b.selectoperatestate
	end as selectoperatestate,
	b.selector,
	b.notdeductionreason,
	b.taxperiod,
	b.transfertax,
	b.invoicestate,
	b.updateinfo_lastchangedby as lastchangedby,
	b.updateinfo_lastchangedon as lastchangedon,
	f.name_chs as taxorgname,
	a.validtax,
	f.namefortax as namefortax,
	a.applytransferprice as applytransferprice,
	a.signstate as signstate,
	a.signperson,
	a.signtime,
	a.signtype,
	a.transferprice as transferprice,
	a.expandone,
	a.expandtwo,
	a.expandthree,
	a.expandfour,
	a.expandfive,
	a.updateinfo_createdon,
	a.updateinfo_createdby,
	a.updateinfo_lastchangedby,
	a.updateinfo_lastchangedon,
	c.vouchernumber,
	c.voucherid,
	c.voucherdate,
	c.voucheryear,
	d.doccode,
	d.docid,
	d.doctypecode,
	d.doctypename,
	d.isnewinvoice,
	d.systemcode,
	d.systemname,
	k.fidepartment,
	k.fiunit,
	k.partner,
	a.projectid,
	a.contractid
from
	taxinvatinvoice a
left join taxininvoicevoucher c on
	A.ID = c.parentid
left join taxininvoicerelation d on
	A.ID = d.parentid
	and d.isnewinvoice = '1'
left join taxininvoiceassist k on
	A.ID = k.parentid
left join bfdftaxorg h on
	a.taxorgid = h.id
left join bfmasterorganization f on
	h.bfmsorg = f.id
left join bfinvoicetype g on
	a.invoicetypeid = g.id
left join taxinvatinvoicededuction b on
	a.id = b.parentid) t
where
	invoicetypecode in('01',
	'14',
	'13',
	'06',
	'85')
	and (t.taxamount > 0
	or t.taxamount = 0);

使用left join时执行计划:
image.png
2.不使用left join时sql:

select
	t.id,
	t.invoicetypename_chs,
	t.invoicetypename_cht,
	t.invoicetypename_en,
	t.invoicetypename_es,
	t.invoicetypename_pt,
	t.invoicetypeid,
	t.invoicetypecode,
	t.taxorgid,
	t.invoicecode,
	t.invoicenumber,
	t.invoicedate,
	t.sourcetype,
	t.amount,
	t.taxamount,
	t.amountandtax,
	t.sellertaxpayername,
	t.sellertaxpayerid,
	/*t.selectconfirmstate,
	t.invveristate,
	t.isdeduct,
	t.istaxreimbursement,
	t.taxreimbursementamount,
	t.administrateid,
	t.candeduction,
	t.note,
	t.selecttype,
	t.selectedtime,
	t.selectoperatestate,
	t.selector,
	t.notdeductionreason,
	t.taxperiod,
	t.transfertax,
	t.taxorgname,*/
	t.validtax,
	--t.namefortax,
	t.applytransferprice,
	t.signstate,
	t.signperson,
	t.signtime,
	t.signtype,
	--t.invoicestate,
	t.transferprice,
	t.expandone,
	t.expandtwo,
	t.expandthree,
	t.expandfour,
	t.expandfive,
	t.updateinfo_createdon,
	t.updateinfo_createdby,
	t.updateinfo_lastchangedby,
	t.updateinfo_lastchangedon,
	/*t.lastchangedby,
	t.lastchangedon,
	t.vouchernumber,
	t.voucherid,
	t.voucherdate,
	t.voucheryear,
	t.doccode,
	t.docid,
	t.doctypecode,
	t.doctypename,
	t.isnewinvoice,
	t.systemcode,
	t.systemname,
	t.fidepartment,
	t.fiunit,
	t.partner,*/
	t.projectid,
	t.contractid
from
	(
	select
		a.id, g.name_chs as invoicetypename_chs, g.name_cht as invoicetypename_cht, g.name_en as invoicetypename_en, g.name_es as invoicetypename_es, g.name_pt as invoicetypename_pt, g.id as invoicetypeid, g.code as invoicetypecode, a.taxorgid, a.invoiceinfo_invoicecode as invoicecode, a.invoiceinfo_invoicenumber as invoicenumber, a.invoiceinfo_invoicedate as invoicedate, a.sourcetype as sourcetype, a.invoiceinfo_amount as amount, a.invoiceinfo_taxamount as taxamount, a.invoiceinfo_amountandtax as amountandtax, a.seller_taxpayername as sellertaxpayername, a.seller_taxpayerid as sellertaxpayerid,
		/*case
			when(b.selectconfirmstate is null) then '0'
			else b.selectconfirmstate
		end as selectconfirmstate, a.invveristate, a.isdeduct, a.istaxreimbursement, a.taxreimbursementamount, a.administrateid, b.candeduction, a.note, b.selecttype, b.selectedtime
		, case when(b.selectoperatestate is null) then '0'
		else b.selectoperatestate
	end as selectoperatestate,
	b.selector,
	b.notdeductionreason,
	b.taxperiod,
	b.transfertax,
	b.invoicestate,
	b.updateinfo_lastchangedby as lastchangedby,
	b.updateinfo_lastchangedon as lastchangedon,*/
	--f.name_chs as taxorgname,
	a.validtax,
	--f.namefortax as namefortax,
	a.applytransferprice as applytransferprice,
	a.signstate as signstate,
	a.signperson,
	a.signtime,
	a.signtype,
	a.transferprice as transferprice,
	a.expandone,
	a.expandtwo,
	a.expandthree,
	a.expandfour,
	a.expandfive,
	a.updateinfo_createdon,
	a.updateinfo_createdby,
	a.updateinfo_lastchangedby,
	a.updateinfo_lastchangedon,
	/*c.vouchernumber,
	c.voucherid,
	c.voucherdate,
	c.voucheryear,
	d.doccode,
	d.docid,
	d.doctypecode,
	d.doctypename,
	d.isnewinvoice,
	d.systemcode,
	d.systemname,
	k.fidepartment,
	k.fiunit,
	k.partner,*/
	a.projectid,
	a.contractid
from
	taxinvatinvoice a
/*left join taxininvoicevoucher c on
	A.ID = c.parentid
left join taxininvoicerelation d on
	A.ID = d.parentid
	and d.isnewinvoice = '1'
left join taxininvoiceassist k on
	A.ID = k.parentid
left join bfdftaxorg h on
	a.taxorgid = h.id
left join bfmasterorganization f on
	h.bfmsorg = f.id*/
left join bfinvoicetype g on
	a.invoicetypeid = g.id
/*left join taxinvatinvoicededuction b on
	a.id = b.parentid*/
	) t
where
	invoicetypecode in('01',
	'14',
	'13',
	'06',
	'85')
	and (t.taxamount > 0
	or t.taxamount = 0);

不使用left join时执行计划:
image.png

回答 0
暂无回答
扫一扫
联系客服