SQL Server: выберите INTO из Pivot, работающего медленно - PullRequest
0 голосов
/ 14 ноября 2018

РЕШЕНО: Я нашел проблему, и ее было легко исправить. На самом деле моя собственная ошибка, журналы для tempdb были на 99%, поэтому я сократил их до 5 МБ, и запрос выполняется за считанные секунды.

Я написал сводку для транспонирования диапазона значений в столбцы. Когда запрос выполняется без вставки данных в какие-либо таблицы, он выполняется менее чем за 1 минуту. Когда я добавляю INTO ## CandidateSummaryTable , это занимает ~ 2 часа и 48 минут. Понятия не имею почему.

Таблица содержит около 1,4 миллиона записей. Я поместил кластеризованный индекс в CandidateID, QuestionAnswerDate и Question, что значительно сократило время, но не достаточно.

Расчетный план запроса предполагает, что это займет еще больше времени, если я

INSERT INTO ##CandidateSumamryTable 
    SELECT ...* 

вместо этого, так что я еще не запускал это.

Предполагаемый план запроса для SELECT INTO ... устанавливает вставку таблицы в 98%.

Есть ли лучший способ транспонировать эти данные?

Это запрос: -

SELECT 
    CandidateID,
    QuestionAnswerDate,
    [Academic Title]
    ,[Address]
    ,[Address2]
    ,[Administrative Information Section]
    ,[AIM]
    ,[Alternative Email Address]
    ,[Assigned Location]
    ,[Availability?]
    ,[Availability]
    ,[Candidate Name Section]
    ,[CitizenOf]
    ,[City]
    ,[City2]
    ,[Class Rank (out of)]
    ,[Class Rank]
    ,[Comments]
    ,[Compensation]
    ,[Contact Information Section]
    ,[Contact Method (preferred)]
    ,[Contractor Information Section]
    ,[Contractor Rates]
    ,[Country]
    ,[Current]
    ,[Currently attending]
    ,[Custom Question]
    ,[Daily Rate Sort]
    ,[Data Privacy Statement]
    ,[Date of Birth]
    ,[Degree Date]
    ,[Degree Major Name]
    ,[Degree Measure]
    ,[Degree Name]
    ,[Degree Type]
    ,[Degree]
    ,[Department Name]
    ,[Department Type]
    ,[Description]
    ,[Education History Section]
    ,[Education History]
    ,[EducationCountry]
    ,[E-mail address]
    ,[Employee Code]
    ,[Employee Information Section]
    ,[Employer Organization Name]
    ,[Employer Organization Type]
    ,[Employer Organization]
    ,[Employment History Section]
    ,[Employment History]
    ,[End Date]
    ,[Ending Compensation Currency]
    ,[Ending Compensation Interval]
    ,[Ending Compensation]
    ,[Facebook]
    ,[FAX]
    ,[First Name]
    ,[Flickr]
    ,[Form of address]
    ,[GaduGadu]
    ,[Gender]
    ,[Google Talk]
    ,[ICQ]
    ,[Jabber]
    ,[Last Name]
    ,[Link To Pool Requision]
    ,[LinkedIn]
    ,[Manager Name]
    ,[Marital Status]
    ,[Middle Name]
    ,[MySpace]
    ,[Notice period]
    ,[Number of Children]
    ,[Orkut]
    ,[Other benefits]
    ,[Personal Information Section]
    ,[Personal Web Site]
    ,[Phone (home)]
    ,[Phone (mobile)]
    ,[Phone (work)]
    ,[Plaxo]
    ,[Position History]
    ,[Position Title]
    ,[Position Type]
    ,[Rate per day]
    ,[Reason for leaving]
    ,[Salary Sort]
    ,[School Name]
    ,[School or Institution]
    ,[School Type]
    ,[Skype]
    ,[Social networking and instant messaging accounts]
    ,[Sourcing And Referral]
    ,[Sourcing Medium]
    ,[SSN]
    ,[Start Date]
    ,[Sub-School / Department]
    ,[Tagged]
    ,[Twitter]
    ,[Viadeo]
    ,[Windows Live Messenger]
    ,[Windows Live Spaces]
    ,[XING]
    ,[Yahoo Messenger]
    ,[ZIP]
INTO 
    ##CandidateSummaryTable
FROM 
    (SELECT CandidateID, Question, QuestionAnswerDate, Answer
     FROM #CandidateSummary) d
PIVOT (MAX(Answer) 
          FOR Question IN ([Academic Title]
    ,[Address]
    ,[Address2]
    ,[Administrative Information Section]
    ,[AIM]
    ,[Alternative Email Address]
    ,[Assigned Location]
    ,[Availability?]
    ,[Availability]
    ,[Candidate Name Section]
    ,[CitizenOf]
    ,[City]
    ,[City2]
    ,[Class Rank (out of)]
    ,[Class Rank]
    ,[Comments]
    ,[Compensation]
    ,[Contact Information Section]
    ,[Contact Method (preferred)]
    ,[Contractor Information Section]
    ,[Contractor Rates]
    ,[Country]
    ,[Current]
    ,[Currently attending]
    ,[Custom Question]
    ,[Daily Rate Sort]
    ,[Data Privacy Statement]
    ,[Date of Birth]
    ,[Degree Date]
    ,[Degree Major Name]
    ,[Degree Measure]
    ,[Degree Name]
    ,[Degree Type]
    ,[Degree]
    ,[Department Name]
    ,[Department Type]
    ,[Description]
    ,[Education History Section]
    ,[Education History]
    ,[EducationCountry]
    ,[E-mail address]
    ,[Employee Code]
    ,[Employee Information Section]
    ,[Employer Organization Name]
    ,[Employer Organization Type]
    ,[Employer Organization]
    ,[Employment History Section]
    ,[Employment History]
    ,[End Date]
    ,[Ending Compensation Currency]
    ,[Ending Compensation Interval]
    ,[Ending Compensation]
    ,[Facebook]
    ,[FAX]
    ,[First Name]
    ,[Flickr]
    ,[Form of address]
    ,[GaduGadu]
    ,[Gender]
    ,[Google Talk]
    ,[ICQ]
    ,[Jabber]
    ,[Last Name]
    ,[Link To Pool Requision]
    ,[LinkedIn]
    ,[Manager Name]
    ,[Marital Status]
    ,[Middle Name]
    ,[MySpace]
    ,[Notice period]
    ,[Number of Children]
    ,[Orkut]
    ,[Other benefits]
    ,[Personal Information Section]
    ,[Personal Web Site]
    ,[Phone (home)]
    ,[Phone (mobile)]
    ,[Phone (work)]
    ,[Plaxo]
    ,[Position History]
    ,[Position Title]
    ,[Position Type]
    ,[Rate per day]
    ,[Reason for leaving]
    ,[Salary Sort]
    ,[School Name]
    ,[School or Institution]
    ,[School Type]
    ,[Skype]
    ,[Social networking and instant messaging accounts]
    ,[Sourcing And Referral]
    ,[Sourcing Medium]
    ,[SSN]
    ,[Start Date]
    ,[Sub-School / Department]
    ,[Tagged]
    ,[Twitter]
    ,[Viadeo]
    ,[Windows Live Messenger]
    ,[Windows Live Spaces]
    ,[XING]
    ,[Yahoo Messenger]
    ,[ZIP]
)) piv
...