Home Database Mysql Tutorial 高访问量的评论系统数据库存储过程架构

高访问量的评论系统数据库存储过程架构

Jun 07, 2016 pm 02:56 PM
storage database Architecture system Views Comment process

网站的评论信息是最耗资源的地方,做好评论数据的优化对大型站点来说至关重要 无 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[CommentsTables]([ID] [int] IDENTITY(1,1) NOT NULL,[Key] [nvarchar](50) NOT NULL,[TableName] [nvarch

网站的评论信息是最耗资源的地方,做好评论数据的优化对大型站点来说至关重要
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[CommentsTables](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Key] [nvarchar](50) NOT NULL,
	[TableName] [nvarchar](80) NOT NULL,
	[StartID] [int] NOT NULL,
	[EndID] [int] NOT NULL,
 CONSTRAINT [PK_SysTables] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO






--根据SourceID和key获得表名
create function [dbo].[funGetTableName]
(
@SourceID int,
@Key nvarchar(50)
)
RETURNS nvarchar(80)
as 
begin
	declare @tableName nvarchar(80);
	declare @tableArea int;
	declare @mod int;
	
	declare @Size int;
	set @Size = 1000;
	
	set @mod = @SourceID % @Size;
	if @mod > 0 
		set @tableArea = Cast((@SourceID-1) / @Size as int) + 1;		
	else	
		set @tableArea = Cast((@SourceID-1) / @Size as int);
			
			
	set @tableName = 'comments_' + @Key +  Cast(@tableArea as nvarchar(10));
	
	return @tableName
end
GO







--评论写入调用存储过程

CREATE proc [dbo].[procAddComment]
(
@ParentID int,
@SourceID int,
@NickName nvarchar(20),
@Content nvarchar(300),
@IP nvarchar(30),
@City nvarchar(30),
@BeFiltered bit,
@Disable bit,
@Key nvarchar(50),
@InsertedID int Output
)
as
begin
	declare @tableName nvarchar(80);
	declare @tableArea int;
	declare @mod int;
	
	declare @Size int;
	set @Size = 1000;
	
	set @mod = @SourceID % @Size;
	if @mod > 0 
		set @tableArea = Cast((@SourceID-1) / @Size as int) + 1;		
	else	
		set @tableArea = Cast((@SourceID-1) / @Size as int);
		


	
	set @tableName = 'comments_' + @Key +  Cast(@tableArea as nvarchar(10));
	
	if not Exists(select 'x' from [CommentsTables] where [Key]=@Key and [TableName]=@tableName)	
	begin
	
		declare @StartID int;
		declare @EndID int;
		
		set @EndID = @tableArea * @Size;
		set @StartID = @EndID - (@Size-1);
	
	
		--创建表
		declare @CreateSQL nvarchar(MAX);
		set @CreateSQL = 
		'Create table [dbo].['+@tableName+'](
		[ID] [int] IDENTITY(1,1) NOT NULL,
		[ParentID] [int] NOT NULL,
		[SourceID] [int] NOT NULL,
		[NickName] [nvarchar](20) NOT NULL,
		[Content] [nvarchar](300) NOT NULL,
		[Datetime] [datetime] NOT NULL,
		[IP] [nvarchar](30) NOT NULL,
		[City] [nvarchar](30) NOT NULL,
		[BeFiltered] [bit] NOT NULL,
		[Disable] [bit] NOT NULL,
		[Lou] [int] NOT NULL,
		[Ding] [int] NOT NULL,
		[Cai] [int] NOT NULL,
		 CONSTRAINT [PK_'+@tableName+'] PRIMARY KEY CLUSTERED 
		(
			[ID] ASC
		)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
		) ON [PRIMARY]'
		
		
		EXEC(@CreateSQL);
		
		
		--创建索引 ID DESC
		EXEC('		
		CREATE UNIQUE NONCLUSTERED INDEX [IX_'+@tableName+'_ID_DESC] ON [dbo].['+@tableName+'] 
		(
			[ID] DESC
		)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]');
		
		--创建索引 Ding DESC
		EXEC('
		CREATE NONCLUSTERED INDEX [IX_'+@tableName+'_Ding_DESC] ON [dbo].['+@tableName+'] 
		(
			[Ding] DESC
		)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]');
		
		--创建索引 SourceID DESC
		EXEC('
		CREATE NONCLUSTERED INDEX [IX_'+@tableName+'_SourceID_DESC] ON [dbo].['+@tableName+'] 
		(
			[SourceID] DESC
		)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]');

		--创建索引 Lou DESC
		EXEC('
		CREATE NONCLUSTERED INDEX [IX_'+@tableName+'_Lou_DESC] ON [dbo].['+@tableName+'] 
		(
			[Lou] DESC
		)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]')
		
		
		--创建默认值
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_ParentID]  DEFAULT ((0)) FOR [ParentID]');
		
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_Datetime]  DEFAULT (getdate()) FOR [Datetime]');
		
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_BeFiltered]  DEFAULT ((0)) FOR [BeFiltered]');	

		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_Disable]  DEFAULT ((0)) FOR [Disable]');
	
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_Lou]  DEFAULT ((1)) FOR [Lou]');
			
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_Ding]  DEFAULT ((0)) FOR [Ding]');
		
		EXEC('ALTER TABLE [dbo].['+@tableName+'] ADD  CONSTRAINT [DF_'+@tableName+'_Cai]  DEFAULT ((0)) FOR [Cai]');
		
		Insert Into [CommentsTables]([Key],[TableName],[StartID],[EndID]) values(@Key,@tableName,@StartID,@EndID);
	end 

	
	declare @TemLou int;
	declare @SQL nvarchar(MAX);
	set @SQL = N'select @TemLou = Count(ID) from dbo.['+@tableName+N'] where SourceID=@SourceID';
	
	exec sp_executesql @SQL,
	N'@SourceID int,@TemLou int output',
	@SourceID,
	@TemLou output;
	
	if @TemLou = 0
		set @TemLou = 1;
	else
		set @TemLou = @TemLou + 1;
	
	
	
	declare @Lou int;
	set @Lou = @TemLou;
	
	declare @InsertSQL nvarchar(MAX);
	set @InsertSQL = N'Insert Into dbo.['+@tableName+N'](ParentID,SourceID,NickName,Content,IP,City,BeFiltered,[Disable],[Lou])
	values (@ParentID,@SourceID,@NickName,@Content,@IP,@City,@BeFiltered,@Disable,@Lou);select @InsertedID = SCOPE_IDENTITY();';
	
	exec sp_executesql @InsertSQL,
	N'@ParentID int,@SourceID int,@NickName nvarchar(20),@Content nvarchar(300),@IP nvarchar(30),@City nvarchar(30),@BeFiltered bit,@Disable bit,@Lou int,@InsertedID int output',
	@ParentID,
	@SourceID,
	@NickName,
	@Content,
	@IP,
	@City,
	@BeFiltered,
	@Disable,
	@Lou,
	@InsertedID output;
end 








GO















--获得最新评论存储过程

CREATE proc [dbo].[procGetNewComments]
(
@SourceID int,
@Key nvarchar(50),
@PageIndex int,
@PageSize int,
@Fields nvarchar(100),
@PageCount int output
)
as
begin
	declare @tableName nvarchar(80);
	set @tableName = dbo.funGetTableName(@SourceID,@Key);
	declare @Rc int;	
	
	declare @SQL nvarchar(MAX);
	set @SQL = N'select @Rc = COUNT(ID) from dbo.['+@tableName+N'] where SourceID = @SourceID';	
	
	exec sp_executesql @SQL,
	N'@SourceID int,@Rc int output',
	@SourceID,
	@Rc output;
	
	if @Rc % @PageSize > 0 
		set @PageCount = Cast(@Rc / @PageSize as int) + 1;
	else
		set @PageCount = Cast(@Rc / @PageSize as int);
		

	
	if @PageIndex = 1 
		begin
			set @SQL = N'select top '+Cast(@PageSize as nvarchar(30))+' '+@Fields + N' from dbo.['+@tableName+N'] where SourceID=@SourceID order by Lou desc';					
			exec sp_executesql @SQL,
			N'@SourceID int',
			@SourceID;
		end 		
	else
		begin
			declare @StartLou int;
			declare @EndLou int;
			
			--1 20  1 - 20,21- 40,41-60
			set @EndLou =  @Rc - (@PageIndex-1) * @PageSize;
			
			if @EndLou > @Rc 
				set @EndLou  = @Rc;			
			
			set @StartLou = @EndLou - @PageSize + 1;
			
			if @StartLou < 1 
				set @StartLou = 1;
			
			
			set @SQL = N'select '+@Fields + N' from dbo.['+@tableName+N'] where Lou>=@StartLou and Lou<=@EndLou and SourceID = @SourceID order by Lou desc';
			
			
			exec sp_executesql @SQL,
			N'@SourceID int,@StartLou int,@EndLou int',
			@SourceID,@StartLou,@EndLou;
		end
	
end




GO











--踩

Create proc [dbo].[procCai]
(
@ID int,
@SourceID int,
@key nvarchar(50),
@Times int output
)
as
begin
	declare @tableName nvarchar(80);
	set @tableName = dbo.funGetTableName(@SourceID,@key);
	
	declare @SQL nvarchar(MAX);
	set @SQL = N'update dbo.['+@tableName+N'] set Cai=Cai+1 where ID=@ID;select @Times=Cai from dbo.['+@tableName+N'] where ID=@ID';
	
	
	exec sp_executesql @SQL,
	N'@ID int,@Times int output',
	@ID,
	@Times output;
end


GO


--顶
create proc [dbo].[procDing]
(
@ID int,
@SourceID int,
@key nvarchar(50),
@Times int output
)
as
begin
	declare @tableName nvarchar(80);
	set @tableName = dbo.funGetTableName(@SourceID,@key);
	
	declare @SQL nvarchar(MAX);
	set @SQL = N'update dbo.['+@tableName+N'] set Ding=Ding+1 where ID=@ID;select @Times=Ding from dbo.['+@tableName+N'] where ID=@ID';
	
	
	exec sp_executesql @SQL,
	N'@ID int,@Times int output',
	@ID,
	@Times output;
end

GO




Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

How to add people who comment on WeChat video accounts as friends? How do you know who the person who commented on the video account is? How to add people who comment on WeChat video accounts as friends? How do you know who the person who commented on the video account is? Apr 01, 2024 pm 05:06 PM

As the functions of WeChat video accounts continue to be upgraded, users have higher and higher requirements for the interactivity of video accounts. Among them, many people hope to find their favorite friends through comments and then add them as WeChat friends. So, how to add people who comment on WeChat video accounts as friends? 1. How to add people who comment on WeChat video accounts as friends? 1. Find the comments from your favorite friends. First, you need to find the comment in the WeChat video account that makes your heart move. This can be achieved by browsing the comment section of the video account. When you find an interesting comment, you can click on the commenter's avatar to enter his personal homepage. 2. Follow the video account and send a private message to the commenter's personal homepage. You can see their WeChat name and avatar. At this time, you can first follow the video account and then click

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

How steep is the learning curve of golang framework architecture? How steep is the learning curve of golang framework architecture? Jun 05, 2024 pm 06:59 PM

The learning curve of the Go framework architecture depends on familiarity with the Go language and back-end development and the complexity of the chosen framework: a good understanding of the basics of the Go language. It helps to have backend development experience. Frameworks that differ in complexity lead to differences in learning curves.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

See all articles