on 2011 Oct 26 10:27 AM
Hello,
We have a query which selects from about 20 tables. Lets say, T is one of these tables (and T.id is a primary key). Let's say, we have a string @S with some T.ids, delimited by some delimiter, let it be a semicolon (e. g.: "1;5;10"). We want to filter a query using this string. We have tried this approach:
T.id IN (select row_value from sa_split_list(@S, ';'))
But the problem is that query optimizer always chooses tablescan. There would not be problems (InList would be chosen) if we used
T.id IN (1, 5, 10)
but it is not possible (we do not know these values). Is it possible to force optimizer to use something like "InList" and to start joining with table T?
We also tried to create a global temporary table and insert these IDs into it before executing the query (and then join that table). Unfortunately, optimizer chooses good plan only when we set optimization_level option to 15 (by default it is 9). Good plan starts with
( IndexOnlyScan tmp_ids tmp_ids** )but then optimization costs ~30 seconds instead of less than 1 second.
Is there a way to force optimizer to always chose to join that global temporary table (index scan) with table T firstly?
SA versions: 11 or 12.
Request clarification before answering.
EXECUTE IMMEDIATE WITH RESULT SET string('select ... T.id IN (', @S, ')')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This approach seems to be the most applicable till now and solves our problem. But the second question still remains (for the future): is there a way to force optimizer to always start scanning from a concrete table (concrete index)? That could reduce optimization cost, methinks.
SQL Anywhere supports index hints on table tables in the FROM clause, using the WITH keyword. See the help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Specifically, see "SELECT ... FROM Table WITH INDEX(index_name)" (or "FORCE INDEX(index_name)") in the help: http://dcx.sybase.com/index.html#1201/en/dbreference/from-statement.html
You could try a user estimate like (T.id in ... ,0.00001) to influence the optimizer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
30 | |
10 | |
8 | |
8 | |
7 | |
7 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.