I持って、次のDBのテーブル:複雑なSQLクエリ、大きなデータベース
BOOKS
book_id | date | library_id
1 | 06 | 34
2 | 02 | 12
3 | 04 | 34
4 | 09 | 66
LIBRARY
library_id | adress | owner
1 | "cxc" | "andf"
2 | "mkm" | "kla"
3 | "ass" | "pol"
4 | "kon" | "ger"
PAGESLLV
page_id | book_id | text
4 | 4 | "YYYY ss"
3 | 1 | "FFF as"
3 | 1 | "FDER fs"
3 | 2 | "GRG xx""
PAGESKYK
page_id | book_id | text
1 | 1 | "ddadad"
2 | 3 | "xcvxcv"
1 | 3 | "adad"
2 | 2 | "ddddweg"
PAGESLOO
page_id | book_id | text
6 | 5 | "VV"
5 | 2 | "CCC"
6 | 2 | "ZZ"
7 | 3 | "AA"
とDBに関するいくつかの情報:
1)全ての書籍が多くのページ
example:
Book with id 622 has:
234 pages with id 45,
120 pages with id 23,
1 page with id 11,
1 page with id 31,
Book with id 322 has:
1 page with id 67,
1 page with id 88
2を持っている)すべての本があり1つのlibrary_id
を持っていますPAGE___という名前の9つのテーブルです(___は"LLV"のようなもの) 約2400万レコードがあります。
今、私は、特定のIDを持つすべてのページを含むすべての書籍(ライブラリのアドレスを含む)を抽出するためにクエリを作成する必要があります。
ので、例えば:
Book with id 622 has:
234 pages with id 45,
120 pages with id 88,
1 page with id 11,
1 page with id 23,
Book with id 13 has:
234 pages with id 88,
120 pages with id 23,
1 page with id 11,
1 page with id 15,
2 pages with id 56,
Book with id 322 has:
1 page with id 23,
1 page with id 88
、彼らは私に配列[88,23,11,15]を得るとid 13との唯一の本が有効であるので、私は
book_id | date | library_adress | library_owner |
13 | ~~~~ | ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~|
を返します。
私は今の私のSQLは、Microsoft SQL Server 2008の
を使用しています:
'with p1 as (
select distinct podv.Book_id, podv.Page_Id
from PAGESLLV podv with (nolock)
where podv.Page_Id in (' + @Ids + ')
union all
select distinct psv.Book_id, psv.Page_Id
from PAGESXXN psv with (nolock)
where psv.Page_Id in (' + @Ids + ')
union all
select distinct psav.Book_id, psav.Page_Id
from PAGESTTY psav with (nolock)
where psav.Page_Id in (' + @Ids + ')
union all
select distinct psx.Book_id, psx.Page_Id
from PASGESPOO psx with (nolock)
where psx.Page_Id in (' + @Ids + ')
union all
select distinct pv.Book_id, pv.Page_Id
from PAGESMIO pv with (nolock)
where pv.Page_Id in (' + @Ids + ')
union all
select distinct tpb.Book_id, tpb.Page_Id
from PAGESQWW tpb with (nolock)
where tpb.Page_Id in (' + @Ids + ')),
p2 as (select p1.Book_id
from p1
group by p1.Book_id
having COUNT(p1.Book_id) = ' + @Amount + ')
select top 1000
r.Book_id,
r.date,
v.adress,
v.owner,
from Books r with (nolock)
inner join p2 with (nolock) on (r.Book_id = p2.Book_id)
join Library v with (nolock) on (r.library_id = v.library_id)
order by r.Book_id')
を、それは動作しますが、遅すぎる
任意の助けてくれてありがとう、と私の英語力のために残念。
だから、あなたがこれまでにしようとしているものを.... – logixologist
http://pastebin.com/1JTbQJjz それは今あまりにも遅いために働く:/ –
RDBMS(ベンダーおよびバージョン)を追加してくださいとecpected出力を追加してください**与えられたサンプルデータにフィッティング**! – Shnugo