sql - INTERSECT is very slow - better solution available? -
i have datababase 1 main table , sub-tables. view data created c# application, shows 1 large table. in application user can set filter, show part of data. filter has 20 different parameters can each used or not. clarity of stored procedure organized this: each of 20 parameters create temporary table:
declare @tbl_parameter1 type_int declare @tbl_parameter2 type_int ...
type_int user-defined table type contains 1 column of type int.
in next step fill these tables ids of main table of rows fit filter-parameter. if 1 parameter not set, fill ids.
id @parameter1 > 0 begin insert @tbl_parameter1 select id tbl_main col1 = @parameter1 end else begin insert @tbl_parameter1 select id tbl_main end
so in end need ids occure in every of 20 temporary tables: step slow.
insert @result select value @tbl_testsequencetags intersect select value @tbl_source intersect select value @tbl_vehicle ... (17 more tables)
at end stored procedure provides data c# application , contains joins , unions.
exec get_data @result
is there better (faster) solution creating @result-table? or should filtering in complete other way?
why not write query using 1 pass?
select id tbl_main (col1 = @parameter1 or @paramter1 null) , (col2 = @parameter2 or @paramter2 null) , . . .;
this require scanning original table. however, should more efficient writing 20 temporary tables -- many of them ids rows in original data.
Comments
Post a Comment