sql - Creating and using a temporary table to return a specific row, instead of running the query repeatedly -


i have program query being run repeatedly single row number (here called "partition").

declare @partition int = {0}  ;with candidates (     select column1, column2,     row_number() on (order table1.column1) rownumber,     ... ) select c.column1, c.column2,  candidates c c.rownumber - 1 = @partition 

instead of running query every time want specific row, i'd query write temporary table , return first row if partition zero, , return row temporary table if partition not zero.

how go doing that? can done in single query?

i not stated question why not

select column1, column2, ... order column1  offset @partition rows  fetch next 1 rows only; 

is complex query want cache?

ok cache

declare @partition int = {0}  if @partition = 0 begin     truncate table tablecache;    insert tablecache (column1, column2, rownumber)     select column1, column2,            row_number() on (order table1.column1, column2) rownumber     ......; end select column1, column2    tablecache   rownumber - 1 = @partition;  

if program way more efficient read column1, column2 list , refer them program


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -