|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, December 12, 2012 1:18 PM
Points: 17,
Visits: 88
|
|
Hi I’m having a problem with a union of two table valued function, it’s very weird I don’t know if anyone some time had the same problem. I make two table valued functions that’s works fine. But the problem came when I make a third function that’s have a union of the two first one´s : SELECT * FROM dbo.estadisticas_ventas_facturas_clientes(@id_empresa,@id_sucursal,@inicio,@fin,@cliente1,@cliente2,@clasificacion,@activo,@ventas1,@ventas2,@id_vendedor) UNION SELECT * FROM dbo.estadisticas_ventas_devoluciones_clientes(@id_empresa,@id_sucursal,@inicio,@fin,@cliente1,@cliente2,@clasificacion,@activo,@ventas1,@ventas2,@id_vendedor)
Then when I execute the select statement of the function, the function stay executing without giving any result. I’m end canceling the query because of the time of execution. Because of this then I try to insert the results sets of each query into a table variable. But the result was the same.
INSERT INTO @temp SELECT * FROM dbo.estadisticas_ventas_facturas_clientes(@id_empresa,@id_sucursal,@inicio,@fin,@cliente1,@cliente2,@clasificacion,@activo,@ventas1,@ventas2,@id_vendedor)
But if instead of that I insert the result set into a temporally table the query works fine
INSERT INTO #temp SELECT * FROM dbo.estadisticas_ventas_facturas_clientes(@id_empresa,@id_sucursal,@inicio,@fin,@cliente1,@cliente2,@clasificacion,@activo,@ventas1,@ventas2,@id_vendedor)
The problem is that I can’t use a temporally table inside a function. I don’t know if this is a bug or is a problem of design in my functions.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, December 12, 2012 1:18 PM
Points: 17,
Visits: 88
|
|
All functions are inline table valued function but there is a couple of sub function in waterfall inside of each functions, (all inline table valued function), maybe this is the problem.
I being using function because of the ease of work with filters of the query, I think I can use temporal table directly from the application I’m developing instead of put all in just one function.
Or reading the article http://sqlinthewild.co.za/index.php/2008/08/12/views-or-functions/ I gone a try use a stored procedure too, the think is that the information is used in a Crystal Report and I never tray to use a stored procedure as a source I don’t know if is possible.
If is this is helpful I attach the execution plan in the replay.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, August 29, 2012 3:57 PM
Points: 17,
Visits: 77
|
|
I'm seeing a related issue in that when I try to use an ITVF as the source data for an INSERT INTO a table (using minimally logged operations), where the ITVF is at the root of a hierarchy of nested ITVFs, the performance of the INSERT INTO totally sucks. We're talking fairly large resultsets (6-12 million records).
I'm scratching my head, and I've been at this for some time.
Any comments or thoughts.
Respectfully,
Darryll
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
Darryll Petrancuri (1/11/2011) I'm seeing a related issue in that when I try to use an ITVF as the source data for an INSERT INTO a table (using minimally logged operations), where the ITVF is at the root of a hierarchy of nested ITVFs, the performance of the INSERT INTO totally sucks. We're talking fairly large resultsets (6-12 million records).
I'm scratching my head, and I've been at this for some time.
Any comments or thoughts.
Respectfully,
Darryll
Nesting that is like nesting views. At some point the optimizer is overwhelmed and just comes up with whatever plan it can. It's a bad design approach that does lead to issues like you're experiencing.
---------------------------------------------------- "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt The Scary DBA Author of: SQL Server 2012 Query Performance Tuning SQL Server 2008 Query Performance Tuning Distilled and SQL Server Execution Plans
Product Evangelist for Red Gate Software
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, August 29, 2012 3:57 PM
Points: 17,
Visits: 77
|
|
I'm only nesting a few levels deep. And I know it's like nesting views. And when I execute the function via:
SELECT TOP 1 * FROM dbo.udf_...
or
SELECT COUNT(*) FROM dbo.udf_...
The performance is reasonable. It's when I do the
INSERT INTO <table> SELECT <column-list> FROM dbo.udf_...
that the performance goes to hell well beyond what seems reasonable in comparison to if the same ITVF had been written to utilize # temporary tables that are pulled together using similar logic as what occurs using the nested ITVFs
Respectfully,
Darryll
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, December 12, 2012 1:18 PM
Points: 17,
Visits: 88
|
|
| By the way, I did work around this problem replacing the ITVF with store procedures, I don't know exactly way, but talking about performance, it's better a sp. I could use a lot of nested sp and make a more DRY design. I think maybe its because in the sp I can use temporary tables that use less memory and the SQL Server can make a better execution plan. The disadvantage is that sp are a bit more dificult to write and less maintainable than ITVF's
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
Ramón Ontiveros (1/11/2011) By the way, I did work around this problem replacing the ITVF with store procedures, I don't know exactly way, but talking about performance, it's better a sp. I could use a lot of nested sp and make a more DRY design. I think maybe its because in the sp I can use temporary tables that use less memory and the SQL Server can make a better execution plan. The disadvantage is that sp are a bit more dificult to write and less maintainable than ITVF's
yeah, without seeing the code for the functions or the proc, I couldn't tell you why either. But when you're trying to figure this stuff out, the answer usually can be found in the execution plans. That's where I'd go to understand why something is running slow.
---------------------------------------------------- "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt The Scary DBA Author of: SQL Server 2012 Query Performance Tuning SQL Server 2008 Query Performance Tuning Distilled and SQL Server Execution Plans
Product Evangelist for Red Gate Software
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 11:35 AM
Points: 3,672,
Visits: 5,172
|
|
I advise my clients to avoid ALL UDFs of ALL flavors simply because there are WAY too many ways to get in trouble with them.
Best,
Kevin G. Boles SQL Server Consultant SQL MVP 2007-2012 TheSQLGuru at GMail
|
|
|
|