추가 절에 의해서만 다른 두 쿼리 중 하나를 실행하는 아래 조건이 있습니다.
&& acc.Fb_DataSourceKey == dskId
여기에 조건부
var statData = dskId != -1 ? from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
&& acc.Fb_DataSourceKey == dskId
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res
:
from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res;
이제 https://stackoverflow.com/a/2850048/1170932 덕분에 아래로 변경할 수 있음을 알고 있습니다.
var statData = from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res;
if (singleDsk)
statData = from s in statData where s.Key.Fb_DataSourceAdAccountId == dskId select s;
이렇게하면 코드 중복이 제거되지만 끔찍하게 비효율적 인 외부 조인이 사용됩니다 (이해할 만하게도 실제로). FTR 우리는 DB에 MySQL을 사용하고 있습니다.
외부 조인은 안타깝게도 허용 할 수 없을 정도로 느립니다. 외부 조인을 생성하지 않고 코드를 복제하지 않고 쿼리 스타일 조건 을 수행하는 방법이 있습니까?
조인을 수행 하기 전에 필터 를 수행하십시오. 이후가 아닙니다.
var accounts = dao.fb_datasourceadaccount.AsQueryable();
if(dskId != -1)
accounts = accounts.Where(acc => acc.Fb_DataSourceKey == dskId);
var statData = from s in dao.fb_statdata
join acc in accounts
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
group new { s, acc.Fb_DataSourceKey }
by new { s.Fb_DataSourceAdAccountId, s.Start_time }
into res
select res;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다