Hello there,
I'm having some difficulties understanding this post and, therefore, how to run some queries that i have with the sp_executesql. These queries are throwing SLOW SQL in the log of Service Center. For instance, i have this query:
SELECT SUM ({SPS_LINFACT2}.[Qtd]) as Soma FROM {SPS_LINFACT2} JOIN {SPS_FACTURAS} ON {SPS_LINFACT2}.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact] WHERE {SPS_LINFACT2}.[NumUniBen] = @InNUB AND {SPS_LINFACT2}.[CodCSaude] = @InCodCS AND {SPS_LINFACT2}.[DtaCSaude] = @DtaCsaude AND {SPS_LINFACT2}.[IndSituac] <> 'R' AND {SPS_LINFACT2}.[IndSituac] <> 'E' AND {SPS_LINFACT2}.[IdLinFact] <> @InCodLinha AND {SPS_FACTURAS}.[IdEstadoDoc] <> @EstadoFicheiroCarregado AND {SPS_FACTURAS}.[IdEstadoDoc] <> @EstadoDevolvida AND {SPS_FACTURAS}.[IdEstadoDoc] <> @EstadoEmValidacao AND {SPS_FACTURAS}.[IdCodFact] <> @IdCodFact
What do i have to modify to run this query in SQL Server Management Studio like is ran on the Outsystems Platform?
Thank you in advance and sorry for commenting this year old post.
Best regards,
Rafael Valente
This will convert the original query into (almost) the equivalent execute, you then just need to edit the params at the end and you're good to go :-)SELECT CONCAT('EXEC SP_EXECUTESQLN''',REPLACE(SUBSTRING(t, CHARINDEX('SELECT',t),LEN(t)),'''',''''''),''',N''',SUBSTRING(t, 2, CHARINDEX('SELECT',t)-3),''',',REPLACE(REPLACE(REPLACE(REPLACE(SUBSTRING(t, 2, CHARINDEX('SELECT',t)-3),' int','=0'),' nvarchar','='''''),' datetime','=''1900-01-01 00:00:00'''),',',',')) FROM(SELECT [st].[text] AS tFROM sys.dm_exec_cached_plans cpCROSS APPLY sys.dm_exec_sql_text ( cp.plan_handle ) stCROSS APPLY sys.dm_exec_query_plan ( cp.plan_handle ) qpwhere st.text not like '%do not remove this comment%' /* add your search condition below */and st.text like '%SEARCH%'/* add your search condition above */) p
Hello Acácio,
Thank you for the tip! It really helped me handling a performance issue executing a series of advanced queries.
Although, I have one question regarding the first topic on Oracle DB’s (Use the same sorting criteria as the platform (NLS_COMP and NLS_SORT)):
Thank you,
Telmo Oliveira
Telmo Oliveira wrote:
Hi Telmo
Thank you for the feedback! I'm glad you found it helpful!
Regarding your question, you will usually want to optimize your queries to the reality of your database. The indexes created by the platform are NLS_COMP=LINGUISTIC. So, even if your query today might become more performant by using the NLS_SORT of SQL Developer (NLS_COMP=BINARY), as your system evolves you will start getting worst performance. The fact your query performs better under "the wrong" NLS_COMP is an accident - by chance, Oracle is getting a plan that is less worse than the one it had. It is still not a good one.
Let me share a story from "back in the days" when I was doing Support (I believe it's from 2009 or 2010). A colleague from OutSystems was struggling with this exact problem: his query would run in the platform and take 2 minute-something (close to 3), while it would take 30 seconds in SQL Developer. It was still a huge amount of time, but he was insisting he needed to force the NLS_COMP to BINARY on the platform.
I worked with him on that query (an Advanced SQL with subqueries) and we understood that Oracle was making a very bad decision for the subquery. That subquery returned a very small result-set (a few hundred records) and my colleague told me that, by business restrictions, it would never return a huge result-set. Still I played safe and decided to add a "WHERE ROWNUM < 500000" (yes, 5-hundred-thousand) to that subquery.
The result? The query went down to a few dozen milliseconds (less than 100ms), both with NLS_COMP=BINARY and NLS_COMP=LINGUISTIC.
Where am I going with this? If your query is misperforming, and misperforms less in SQL Developer than in the platform, chances are you can optimize performance and slash it to 10% or less of the time in SQL Developer by keeping NLS_COMP=LINGUISTIC and analyzing the query text.
If you find an experienced DBA, you'll see the story above is not a fantasy nor rare... Oracle does a great job at optimizing queries, but sometimes... it fails.
Regards,Acácio
Hi @AcacioPN
We tried to reproduce platform behavior from TOAD. Even after setting "ALTER SESSIONS ", it's doing Full table scan means indexing not working on text column.
Index:
Query we executed:
Explain plan:
Kindly advise. Thank you
Regards,
Muhammad Mahmudul Hasan