由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 请教一个query 优化的问题(filter keyword)
相关主题
请教一个sql问题问个简单的sql语句
请问sql 有条件性的select columnssql query help
菜鸟问题,急A rookie's query question
sql 题目求助问个sql/ ssis的问题 谢谢!
谁能帮我看看这个Insert语句要怎么改一下?一个oracle performance 的问题。
请教一个SQL的问题高手请进
sort two same tables SQL but different resultsany group function reutrn null if there is null value?
SQL copy a table into a new table and add a new column如何除去duplicate rows?
相关话题的讨论汇总
话题: table话题: select话题: keyword话题: col3话题: col2
进入Database版参与讨论
1 (共1页)
s***s
发帖数: 1301
1
我有两个个表Table A 有3个column Col1, Col2, Col3,和Keyword Table B it has
keyword1, keyword2, keyword3.....
我想查Table A 中有哪些record, 其任何一个column (Col1, or Col2, or Col3) 有
任何一个Table B 中keyword 。
我想到的方法是:
利用cursor 来获取一个一个keyword, 然后传到@p1 下面的dynamic query来查取
select *
from A
where Col1 like '%@p1%' or Col2 like '%@p1%' or Col3 like '%@p1%'
问题是我的keyword table可能有上千个,这种方法就比较低效。
请问有什么比较好的方法来实现呢?
谢谢!
B*****g
发帖数: 34098
2
用like '%%', index就废了

【在 s***s 的大作中提到】
: 我有两个个表Table A 有3个column Col1, Col2, Col3,和Keyword Table B it has
: keyword1, keyword2, keyword3.....
: 我想查Table A 中有哪些record, 其任何一个column (Col1, or Col2, or Col3) 有
: 任何一个Table B 中keyword 。
: 我想到的方法是:
: 利用cursor 来获取一个一个keyword, 然后传到@p1 下面的dynamic query来查取
: select *
: from A
: where Col1 like '%@p1%' or Col2 like '%@p1%' or Col3 like '%@p1%'
: 问题是我的keyword table可能有上千个,这种方法就比较低效。

s***s
发帖数: 1301
3
是啊,有啥好办法呢?
现在table A 做batch 处理。 每次populate 大概20个record, run之后,再load 20个
record... :(

【在 B*****g 的大作中提到】
: 用like '%%', index就废了
i*******d
发帖数: 81
4
you did not tell us how big table A is.

【在 s***s 的大作中提到】
: 我有两个个表Table A 有3个column Col1, Col2, Col3,和Keyword Table B it has
: keyword1, keyword2, keyword3.....
: 我想查Table A 中有哪些record, 其任何一个column (Col1, or Col2, or Col3) 有
: 任何一个Table B 中keyword 。
: 我想到的方法是:
: 利用cursor 来获取一个一个keyword, 然后传到@p1 下面的dynamic query来查取
: select *
: from A
: where Col1 like '%@p1%' or Col2 like '%@p1%' or Col3 like '%@p1%'
: 问题是我的keyword table可能有上千个,这种方法就比较低效。

s***s
发帖数: 1301
5
每次load 20 个record进入table A, 来检测这个20个record 中3个column中是否有任
何一个keyword 在table B (大概有1000个keyword)

【在 i*******d 的大作中提到】
: you did not tell us how big table A is.
s**********o
发帖数: 14359
6
做三个FULL-TEXT INDEX在三个COLUMN上?
j******o
发帖数: 13
7
Could you first convert the table B to an one column table?
Like create a table C, it only have one column, and each row is one keyword
from table B?
i*******d
发帖数: 81
8
想到的几点:
1.A 的三个column合并成一个。这样只要比较一次。中间加个特殊字符,防止引入不该
有的match
COALESCE(col1,'') + '|' + COALESCE(col2,'') + '|' + COALESCE(col3,'')
2.可以对新的column加full text index
3.对cursor循环加终止条件,找到match了就终止。可以省一半时间。
你可以先试试1+3.

【在 s***s 的大作中提到】
: 我有两个个表Table A 有3个column Col1, Col2, Col3,和Keyword Table B it has
: keyword1, keyword2, keyword3.....
: 我想查Table A 中有哪些record, 其任何一个column (Col1, or Col2, or Col3) 有
: 任何一个Table B 中keyword 。
: 我想到的方法是:
: 利用cursor 来获取一个一个keyword, 然后传到@p1 下面的dynamic query来查取
: select *
: from A
: where Col1 like '%@p1%' or Col2 like '%@p1%' or Col3 like '%@p1%'
: 问题是我的keyword table可能有上千个,这种方法就比较低效。

s***s
发帖数: 1301
9
多谢您的建议, 我来试试看。

【在 i*******d 的大作中提到】
: 想到的几点:
: 1.A 的三个column合并成一个。这样只要比较一次。中间加个特殊字符,防止引入不该
: 有的match
: COALESCE(col1,'') + '|' + COALESCE(col2,'') + '|' + COALESCE(col3,'')
: 2.可以对新的column加full text index
: 3.对cursor循环加终止条件,找到match了就终止。可以省一半时间。
: 你可以先试试1+3.

B*****g
发帖数: 34098
10
你这是假设keyword没有|

【在 i*******d 的大作中提到】
: 想到的几点:
: 1.A 的三个column合并成一个。这样只要比较一次。中间加个特殊字符,防止引入不该
: 有的match
: COALESCE(col1,'') + '|' + COALESCE(col2,'') + '|' + COALESCE(col3,'')
: 2.可以对新的column加full text index
: 3.对cursor循环加终止条件,找到match了就终止。可以省一半时间。
: 你可以先试试1+3.

i*******d
发帖数: 81
11
我给了一个例子而已。意识是说要用类似方法避免引入本来不存在的match。
总有keyword用不到的特殊字符吧。

【在 B*****g 的大作中提到】
: 你这是假设keyword没有|
i*****w
发帖数: 75
12
建议一种方法:
1) UNPIVOT TableA, You will get:
ID1, COL1, CONTENT1
ID2, COL2, CONTENT2
ID3, COL3, CONTENT3
2) Join TableA and TableB by LIKE.
EXAMPLE:
-- Prepare Source Table
DECLARE @tblA Table (ID int identity, col1 varchar(100), col2 varchar(100),
col3 varchar(100))
INSERT INTO @tblA (col1, col2, col3)
SELECT 'this is a test', 'I am not sure', 'Give it a try.'
UNION ALL
SELECT 'who cares', 'No one knows', 'Why not'
UNION ALL
SELECT 'it is impossible', 'please let me know', 'be honest'
-- Prepare Keyword Table
DECLARE @tblB Table (KW varchar(10))
INSERT INTO @tblB(KW)
SELECT 'su'
UNION
SELECT 're'
UNION
SELECT 'am'
UNION
SELECT 'ok'
UNION
SELECT 'ou'
UNION
SELECT 'ur'
UNION
SELECT 'hones'
-- UnPivot Source TableA
DECLARE @tblResult Table(ID int, ColName varchar(20), Content varchar(100))
INSERT INTO @tblResult
select ID,
COL,
unPvt.Content
FROM @tblA
UNPIVOT (Content For Col in (Col1, Col2, Col3)) unPvt
-- Source Table
SELECT * FROM @tblA
-- Keywords Table
SELECT * FROM @tblB
-- Result Table
SELECT * FROM @tblResult a
INNER JOIN @tblB b on a.Content LIKE '%' + b.KW + '%'
ORDER BY a.ID, ColName
s***s
发帖数: 1301
13
非常感谢,写得如此详细!

,

【在 i*****w 的大作中提到】
: 建议一种方法:
: 1) UNPIVOT TableA, You will get:
: ID1, COL1, CONTENT1
: ID2, COL2, CONTENT2
: ID3, COL3, CONTENT3
: 2) Join TableA and TableB by LIKE.
: EXAMPLE:
: -- Prepare Source Table
: DECLARE @tblA Table (ID int identity, col1 varchar(100), col2 varchar(100),
: col3 varchar(100))

1 (共1页)
进入Database版参与讨论
相关主题
如何除去duplicate rows?谁能帮我看看这个Insert语句要怎么改一下?
SQL Query Question请教一个SQL的问题
请教一个SQL Server的面试题sort two same tables SQL but different results
SQL select one value column for each distinct value another (转载)SQL copy a table into a new table and add a new column
请教一个sql问题问个简单的sql语句
请问sql 有条件性的select columnssql query help
菜鸟问题,急A rookie's query question
sql 题目求助问个sql/ ssis的问题 谢谢!
相关话题的讨论汇总
话题: table话题: select话题: keyword话题: col3话题: col2