Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vantive_98
Starting Member
2 Posts |
Posted - 2002-11-12 : 14:51:29
|
| How to use hint OPTION(FORCE ORDER) inside user-definde function?I want to force optimizer use specific table order in my function.This logic ( force table order) should be inside !!! my function,so user of this function should use it like this select * from my_func('a')Problem: I have compilation error :create table a(i int ,val char )create table b( i int, data char)gocreate function my_func (@v char) returns table as return ( select b.* from b JOIN a ON a.id=b.id OPTION(FORCE ORDER) )goOutput:Server: Msg 156, Level 15, State 1, Procedure my_func, Line 5Incorrect syntax near the keyword 'OPTION'.Any idea? |
|
|
burbakei
Yak Posting Veteran
80 Posts |
Posted - 2002-11-13 : 13:18:53
|
| you can convert your inline table-valued function to multi statementtable-valued functionCREATE FUNCTION my_func (@v char) RETURNS @ForceOrder TABLE (i int, data char) AS BEGIN INSERT INTO @ForceOrder SELECT b.i, b.data FROM b INNER JOIN a ON a.i = b.i OPTION(FORCE ORDER) RETURN END GO can you tell me what is the purpose of @v parameter of this function? |
 |
|
|
vantive_98
Starting Member
2 Posts |
Posted - 2002-11-13 : 15:23:07
|
| Thank you. I did not use parameter in this example to simpify real query. Question based on previous solution : what is performance penalty for using proposed approach ? Does it generate one more additional table ? Thanks ! |
 |
|
|
burbakei
Yak Posting Veteran
80 Posts |
Posted - 2002-11-13 : 19:44:18
|
| Unfortunately i could not find anything about comparing the performance between these two types of functions in books online or any other resource. so when i come to this situation i always createsome benchmark to compare them. you can adopt the following script to evaluate your problem in question.SET NOCOUNT ONGOCREATE TABLE test (i INT)GODECLARE @i INTSET @i = 0WHILE @i < 100000BEGIN INSERT INTO test VALUES (@i) SET @i = @i + 1ENDGOCREATE FUNCTION InLineTableFunc()RETURNS TABLEASRETURN (SELECT * FROM test)GOCREATE FUNCTION MultiStatementTableFunc()RETURNS @Tbl TABLE (i INT)ASBEGIN INSERT INTO @Tbl SELECT * FROM test RETURNENDGOSET STATISTICS TIME ONGOSELECT *FROM InLineTableFunc()SELECT *FROM MultiStatementTableFunc()and then comapre the elapsed time for each select.Interestingly i usually get two almost equal numbers for each elapsed time.by the way, it is good time to tell you that using a FUNCTION TABLEusually has a better perfomace than using temporary tables, due to locking considerations, no matter which type of FUNCTION TABLE.if you had any other question you can mail to burbakei@yahoo.comgood lockEdited by - burbakei on 11/13/2002 20:15:20 |
 |
|
|
|
|
|
|
|