<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlserver.ro/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>T-SQL</title><link>http://sqlserver.ro/forums/15/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Re: optimizare t-sql  sql server 2000</title><link>http://sqlserver.ro/forums/thread/5885.aspx</link><pubDate>Sun, 19 Oct 2008 13:43:52 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:5885</guid><dc:creator>B_gd_n[ ]Sahlean</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/5885.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=5885</wfw:commentRss><description>Din cele scrise de tine structura incompletă a celor două tabele este:&lt;br&gt;TBL_ACC(...,CustomerId,OpeningDate,...)&lt;br&gt;LGX(...,CUSTOMER,CZL,JDATA,...)&lt;br&gt;&lt;br&gt;O posibilă soluţie PARŢIALĂ ar fi să denormalizezi schema bazei de date.&lt;br&gt;Dacă între cele două tabele ai o legătură de tip 1 =&amp;gt;&amp;gt; n(mai multe) la modul LGX 1 =&amp;gt;&amp;gt; n TBL_ACC atunci adaugi în TBL_ACC câmpurile din LGX care prezintă interes şi vei obţine:&lt;br&gt;TBL_ACC(...,CustomerId,OpeingDate,...,CZL,JDATA), denormalizare însoţită de implementarea unor proceduri sub formă de trigger (declanşator) pe LGX care să actualizeze CZL şi JDATA din TBL_ACC.&lt;br&gt;&lt;br&gt;Posibilul avantaj: scapi de joncţiunea de la al doilea INSERT.&lt;br&gt;</description></item><item><title>Re: optimizare t-sql  sql server 2000</title><link>http://sqlserver.ro/forums/thread/5884.aspx</link><pubDate>Sat, 18 Oct 2008 19:26:30 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:5884</guid><dc:creator>rremus</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/5884.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=5884</wfw:commentRss><description>Potzi sa setezi SET STATISTICS IO ON si sa rulezi, apoi sa postezi rezultatul? Deasemenea DDL-ul complet pentru DATA..LGX si DATA..TBL_ACC.&lt;br&gt;&lt;br&gt;In primul rind schimba @data in DATETIME.&lt;br&gt;&lt;br&gt;Din cite inteleg eu primul insert scaneaza complet DATA..LGX si din cele 30 mil. de inregistrari candidat, 23 mil. se califica sa fie inserate. Potzi sa te uitzi in planul de executie si sa ne spui ce metoda de access este aleasa pentru DATA..LGX? Ma astept ca este un clustered index scan. Daca tabela DATA..LGX este 'larga' (multe cimpuri, fiecare record are sute de bytes) atunci te-ar putea ajuta un covering index pe JDATA cu CUSTOMER si CZL included (acest index ar reduce numarul de pagini scanate). Daca LGX are recorduri mici, atunci te-ar putea ajuta sa-l organizezi sa fie clustered dupa JDATA (dar asta are implicatzii in alte operatiuni care le faci pe LGX care nu le putem stii din post).&lt;br&gt;&lt;br&gt;A doua problema este al doilea insert, care banuiesc ca se face ca un nested loop (pentru fiecare TBL_ACC se scaneaza complet #tmp_LGX). Recomandarea mea ar fi sa adaugi un index pe #tmp_LGX pe CustomerCode, sau declara tabela #tmp_LGX ca fiind clustered dupa CustomerCode. Acest index ar ajuta foarte mult nested loop-ul respectiv. Trebuie sa experimentezi aici, sa vezi cum afecteaza primul insert, poate e mai bine sa lasi #tmp_LGX ca heap la inceput si apoi s-o convertesti intr-un cluster pe CustomerCode.&lt;br&gt;</description></item><item><title>Re: optimizare t-sql  sql server 2000</title><link>http://sqlserver.ro/forums/thread/5878.aspx</link><pubDate>Fri, 17 Oct 2008 08:45:12 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:5878</guid><dc:creator>Alex</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/5878.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=5878</wfw:commentRss><description>&lt;P&gt;&lt;STRONG&gt;Care dintre query-uri este cel mai lent (de fapt, cât durează fiecare query) ?&lt;BR&gt;&lt;/STRONG&gt;primul insert dureaza ~ 12 min&lt;BR&gt;al 2-lea insert dureaza ~ 40 min&lt;BR&gt;primul update dureaza ~&amp;nbsp;1 sec&lt;BR&gt;al 2-lea update e&amp;nbsp;rapid ~ 1 sec&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;STRONG&gt;Dacă cel problematic este primul UPDATE, atunci probabil un index pe coloana CustomerCode (în tabela #tmp_LGX_J şi în tabela TEST) ar fi foarte folositor.&lt;BR&gt;&lt;/STRONG&gt;am creat: CREATE INDEX [ix_CustomerCode] ON [dbo].[#tmp_LGX_J]([CustomerCode]) WITH&amp;nbsp; FILLFACTOR = 90 ON [PRIMARY]&lt;BR&gt;trebuie cumva sa-l creez dupa ce fac insertul in tabela asta temporara?&lt;BR&gt;pe tabela TEST este creat index pe CustomerCode.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Bănuiesc că în tabela TBL_ACC există deja index pe CustomerCode (dacă nu există, ar trebui creat şi el).&lt;/STRONG&gt;&lt;BR&gt;DA. pe TBL_ACC exista index pe CustomerID&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;STRONG&gt;Cam câte rânduri (din cele 30 milioane) se inserează în prima tabelă temporară şi în a doua tabelă temporară ?&lt;BR&gt;&lt;/STRONG&gt;in prima tabela se insereaza 23.569.043 in a 2-a 47.553&lt;/P&gt;
&lt;P&gt;mai specific si ca in tabela TEST nu am decat 30 de inregistrari dar pe live o sa am ~ 50.000, nici nu vreau sa ma gandesc atunci :(&lt;/P&gt;
&lt;P&gt;multumesc.&lt;/P&gt;
&lt;P&gt;alex.&lt;/P&gt;</description></item><item><title>Re: optimizare t-sql  sql server 2000</title><link>http://sqlserver.ro/forums/thread/5877.aspx</link><pubDate>Fri, 17 Oct 2008 07:33:35 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:5877</guid><dc:creator>rsocol</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/5877.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=5877</wfw:commentRss><description>Care dintre query-uri este cel mai lent (de fapt, cât durează fiecare query) ? Dacă cel problematic este primul UPDATE, atunci probabil un index pe coloana CustomerCode (în tabela #tmp_LGX_J şi în tabela TEST) ar fi foarte folositor. Bănuiesc că în tabela TBL_ACC există deja index pe CustomerCode (dacă nu există, ar trebui creat şi el). Cam câte rânduri (din cele 30 milioane) se inserează în prima tabelă temporară şi în a doua tabelă temporară ? În funcţie de asta, ar putea fi util să eliminăm una din tabelele temporare sau ambele şi să facem UPDATE-ul într-un singur pas.&lt;br&gt;&lt;br&gt;Răzvan&lt;br&gt;</description></item><item><title>optimizare t-sql  sql server 2000</title><link>http://sqlserver.ro/forums/thread/5876.aspx</link><pubDate>Fri, 17 Oct 2008 06:55:06 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:5876</guid><dc:creator>Alex</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/5876.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=5876</wfw:commentRss><description>&lt;P&gt;salut am o problema cu codul asta...ma poate ajuta cineva, cum as putea sa optimizez ce incerc sa fac mai jos....un sfat ceva&lt;/P&gt;
&lt;P&gt;declare @Data&amp;nbsp;&amp;nbsp;&amp;nbsp;varchar(20)&lt;BR&gt;select @Data = convert(varchar, convert(datetime, dateadd(day, -1, getdate()), 112), 112)&lt;/P&gt;
&lt;P&gt;if object_id('tempdb..#tmp_LGX ') is not null drop table #tmp_LGX&lt;BR&gt;&amp;nbsp;create table #tmp_LGX &lt;BR&gt;&amp;nbsp;&amp;nbsp;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CustomerCode&amp;nbsp;int,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CZL&amp;nbsp;&amp;nbsp;money&lt;BR&gt;&amp;nbsp;&amp;nbsp;)&lt;BR&gt;insert into #tmp_LGX&lt;BR&gt;select b.CUSTOMER, b.CZL&lt;BR&gt;from DATA..LGX b&lt;BR&gt;where&amp;nbsp; b.CUSTOMER is not null and b.JDATA &amp;gt;= dateadd(year,-1,@Data) and JDATA &amp;lt;= @Data&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;if object_id('tempdb..#tmp_LGX_J ') is not null drop table #tmp_LGX_J &lt;BR&gt;&amp;nbsp;create table #tmp_LGX_J &lt;BR&gt;&amp;nbsp;&amp;nbsp;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CustomerCode&amp;nbsp;int,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Suma&amp;nbsp;&amp;nbsp;money&lt;BR&gt;&amp;nbsp;&amp;nbsp;)&lt;BR&gt;insert into #tmp_LGX_J&lt;BR&gt;select b.CustomerCode, sum(b.CZL)/ case &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;when datediff(month, min(a.OpeningDate), @Data) &amp;gt;= 1 &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;and datediff(month, min(a.OpeningDate), @Data) &amp;lt;= 12 &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;then datediff(month, min(a.OpeningDate), @Data)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;when datediff(month, min(a.OpeningDate), @Data) = 0 then 1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else 12&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;BR&gt;from #tmp_LGX b, TBL_ACC a &lt;BR&gt;where b.CustomerCode = a.CustomerId&lt;BR&gt;group by b.CustomerCode&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;update a set a.ValoareRulaj = b.Suma &lt;BR&gt;from TEST a, #tmp_LGX_J b &lt;BR&gt;where a.CustomerCode = b.CustomerCode&lt;/P&gt;
&lt;P&gt;update a set Rulaj = case &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;lt; 200 and a.TipClient = 'PF' then 'sub 200 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt;= 200 and a.ValoareRulaj &amp;lt;= 500 and a.TipClient = 'PF' then 'intre 200 si 500 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt; 500 and a.TipClient = 'PF' then 'peste 500 ron pe luna'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;lt; 400 and a.TipClient = 'PFA' then 'sub 400 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt;= 400 and a.ValoareRulaj &amp;lt;= 1000 and a.TipClient = 'PFA' then 'intre 400 si 1000 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt; 1000 and a.TipClient = 'PFA' then 'peste 1000 ron pe luna'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;lt; 2000 and a.TipClient = 'PJ' then 'sub 2000 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt;= 2000 and a.ValoareRulaj &amp;lt;= 20000 and a.TipClient = 'PJ' then 'intre 2000 si 20000 ron pe luna'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;when a.ValoareRulaj &amp;gt; 20000 and a.TipClient = 'PJ' then 'peste 20000 ron pe luna'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;else ''&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;BR&gt;from TEST a&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;mai specific ca tabela LGX de pe baza de date DATA (DATA..LGX) este f mare are 28 GB si vreo 30 milioane de inregistari.&lt;/P&gt;
&lt;P&gt;vreun index pe tabele temporare ar ajuta?&lt;/P&gt;
&lt;P&gt;multumesc.&lt;/P&gt;
&lt;P&gt;alex.&lt;/P&gt;</description></item></channel></rss>