首页 » 数据库 » 阅读文章
标签Tag的数据库设计
–建表(作品表):
if object_id(‘Poesy’,’u’) is not null
drop table Poesy
go
create table Poesy
(
id int identity(1,1),
Title varchar(100),
Tag varchar(100)
)
go
alter table Poesy add constraint pk_Posy primary key(Id)
go
if object_id(‘Tags’,’u’) is not null
drop table Tags
go
create table Tags
(
Id int identity(1,1),
TagName Varchar(30),
TagCount int, –作品数量
TagCreatDate datetime,
TagTips int –访问量
)
go
alter table Tags add constraint pk_Tags primary key(Id)
go
–面向用户的tag
if object_id(‘UserTags’,’u’) is not null
drop table UserTags
go
create table UserTags
(
Id int identity(1,1),
TagUserId int,
TagName Varchar(30),
TagCount int, –作品数量
TagCreatDate datetime,
TagTips int –访问量
)
go
alter table UserTags add constraint pk_UserTags primary key(Id)
go
–用户在增加、修改、删除作品的时候,都要对 tags和UserTags表进行操作,更新两个表里的tag包括的作品数量(如果没有则增加tag)
–造数据到作品表
declare @Number int,@NumberTop int,@TagId int
Set @TagId = 1
Set @number = 1
Set @numberTop = 10000000
while @number<=@numberTop
begin
if @TagId = 100
Set @TagId = 1
INSERT Poesy(Title,Tag)
select ’title’+cast(@number as varchar), ’历史’++cast(@TagId as varchar)+’,铁木真,元朝历史,蒙古历史’+cast(@number as varchar)
Set @number = @number +1
Set @TagId = @TagId +1
end
–建立索引
create index Ix_poesyTag on Poesy(Tag)
–查询 很慢要一分钟以上(1)
with Orderlist as (
select row_number() over(order by id desc) as rownumber,id,title
from poesy
where charindex(‘,蒙古历史10000,’,’,’+tag+’,’)>0
) select RowNumber,Id,Title
from Orderlist
where RowNumber between 1 and 50
–查询很快(2)
with Orderlist as (
select row_number() over(order by id desc) as rownumber,id,title
from poesy
where charindex(‘,元朝历史,’,’,’+tag+’,’)>0
) select RowNumber,Id,Title
from Orderlist
where RowNumber between 1 and 50
评论 共0条 (RSS 2.0) 发表评论