现在我正在使用一个asp页面循环遍历一个大表,看看其中一行是否至少有一个项目,它是LIKE'1D%',但它需要花费大量的时间和它不仅需要'1D',还需要至少30个其他两个角色。是否有可以将其转换为表的SQL查询
感谢
好的家伙可以让你更多地了解我想要做的事情我希望这个查询更快一点
sql2 = "SELECT code FROM products "
set bs = MyConn.Execute(sql2)
do until bs.eof
if not bs.eof then
sql3 = "SELECT code FROM brand"
set ns = MyConn.Execute(sql3)
do until ns.eof
if not ns.eof then
sql = "SELECT TOP 1 sku, MID(sku, 1, 5) AS brand FROM catalog WHERE sku LIKE '"&bs.fields("code")&ns.fields("code")&"%' "
set rs = MyConn.Execute(sql)
do until rs.eof
if not rs.eof then
response.write(rs.fields("brand")&"<br>")
end if
rs.movenext
loop
set rs = Nothing
end if
ns.movenext
loop
set ns = nothing
end if
bs.movenext
loop
set bs = Nothing
输出是这样的 1DZOO 1FBAH 1FDRE 1FGRA 1FRIV 1FSCS 1FSEC 1FSUR 1CALI
答案 0 :(得分:1)
您可以尝试类似
的内容SELECT ... WHERE SUBSTRING(yourfield, 0, 2) IN ('1D', '2D', '3D', ...)
答案 1 :(得分:0)
你的意思是你首先加载记录集中的所有数据,然后在ASP中循环它以过滤它吗?难怪它很慢! SQL类似于
SELECT
Col1, Col2
FROM
Table1
WHERE
SearchCol1 LIKE '1D%'
OR
SearchCol2 LIKE '1D%'
这也不会很快,但会比现在快得多。
答案 2 :(得分:0)
要考虑的另一件事是将Indexed Computed列添加到目录表中,这将允许简单(和索引)查询&amp;更重要的是 - 加入。因为SUBSTRING本质上是确定性的(计算列只能在它们是确定性的情况下被索引),所以这些列在索引时(在合理范围内)也会一样快,例如:
CREATE TABLE [dbo].[tbl_Example](
[ID] [int] NULL,
[SKU] [varchar](50) NULL,
[Computed_Product] AS (substring([SKU],(1),(2))),
[Computed_Brand] AS (substring([SKU],(3),(2)))
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_Computed_BrandCode] ON [dbo].[tbl_Example]
(
[Computed_Brand] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_ComputedProductCode] ON [dbo].[tbl_Example]
(
[Computed_Product] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
INSERT [dbo].[tbl_Example] ([ID], [SKU]) VALUES (1, N'ASdasdasdL')
INSERT [dbo].[tbl_Example] ([ID], [SKU]) VALUES (2, N'1231f2efwdsfas')
INSERT [dbo].[tbl_Example] ([ID], [SKU]) VALUES (3, N'sdf23re2fwdf')