由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 谁给我优化一下把重复的产品下架的SQL
相关主题
[转载] Can anyone interpret this simple SQL?Good query book recommendation needed, thank you
今典问题: 这个Self Query咋写?A Query question
Merge table with one single query?SQL Server Trigger on System Base Table or Catalog View
Problem when using SQL " Insert...." to AutoNumber.求解释
MS T-SQL 问题Access门外汉问题求教
求助:找出现2次及以上的记录SQL Conditional Select
[转载] strong SQL skills?问一个SQL Server的问题
请教2个sql query 问题请教大牛一道有趣的SQL题
相关话题的讨论汇总
话题: products话题: tbl话题: product话题: insert话题: values
进入Database版参与讨论
1 (共1页)
v*******s
发帖数: 450
1
3万个产品目录有重复的,这个个QUERY下架重复的老产品, 转了15分钟了还没完
update zen_products set products_status=0 where products_id not in( select
max(products_id) products_id from zen_products_description group by products
_name)
a9
发帖数: 21638
2
索引?

products

【在 v*******s 的大作中提到】
: 3万个产品目录有重复的,这个个QUERY下架重复的老产品, 转了15分钟了还没完
: update zen_products set products_status=0 where products_id not in( select
: max(products_id) products_id from zen_products_description group by products
: _name)

i*****w
发帖数: 75
3
Use Partition and if you have the ID indexed, it should be very fast. Try
the example here and you can change it according to your table structure.
declare @tbl Table(id INT identity, name varchar(30), isActive bit)
insert into @tbl values('Product One', 1)
insert into @tbl values('Product One', 1)
insert into @tbl values('Product One', 1)
insert into @tbl values('Product Two', 1)
insert into @tbl values('Product Two', 1)
insert into @tbl values('Product Three', 1)
insert into @tbl values('Product Three', 1)
insert into @tbl values('Product Three', 1)
insert into @tbl values('Product Four', 1)
--select * from @tbl
Update a
set a.IsActive = 0
FROM @tbl a
INNER JOIN
(
SELECT ID, RANK() over (partition by name order by id desc) rk from @tbl
) b
ON a.ID = B.ID
WHERE b.rk > 1
select * from @tbl

products

【在 v*******s 的大作中提到】
: 3万个产品目录有重复的,这个个QUERY下架重复的老产品, 转了15分钟了还没完
: update zen_products set products_status=0 where products_id not in( select
: max(products_id) products_id from zen_products_description group by products
: _name)

v*******s
发帖数: 450
4
PK: products_id
any idea?

【在 a9 的大作中提到】
: 索引?
:
: products

g***l
发帖数: 18555
5
zen_products 要有PK PRODUCT_ID, 把select
max(products_id)
into #TEMP_TABLE
products_id from zen_products_description group by products
_name
然后再UPDATE
i*****w
发帖数: 75
6
UPDATE a
SET a.products_status = 0
FROM zen_products a
INNER JOIN
(
SELECT products_id, RANK() over (partition by products_name order by
products_id desc) rk from zen_products_description
) b
ON a.products_id = b.products_id
WHERE b.rk > 1
1 (共1页)
进入Database版参与讨论
相关主题
请教大牛一道有趣的SQL题MS T-SQL 问题
紧急求助, 关于SQL Server求助:找出现2次及以上的记录
如何把某些行的某个列(unique)的值加1?[转载] strong SQL skills?
query estimation shows cost 900%?请教2个sql query 问题
[转载] Can anyone interpret this simple SQL?Good query book recommendation needed, thank you
今典问题: 这个Self Query咋写?A Query question
Merge table with one single query?SQL Server Trigger on System Base Table or Catalog View
Problem when using SQL " Insert...." to AutoNumber.求解释
相关话题的讨论汇总
话题: products话题: tbl话题: product话题: insert话题: values