比如,交易所的惩罚机制,在他们模棱两可的时候,我首先要问清楚他们为什么要惩罚机制(因为想要避免玩家一直不停的上架下架),然后通过这个为什么想出一个双方都会满意的解决方案。
2,尽量少用join table指令在database
尽量减少没有必要的join table,因为这样很容易产生deadlock,因为涉及很多的table,不用太担心一个table有太多的column,但是要记得给一些column做index,这样会比较好进行搜索。
说到index,index是可以一次做多个column的,并且还可以include column(这里放select的内容),但是要注意include column里面不可以有index
3, database中尽量不要用到xml,这很重
可以用int之类的存,最好不用xml
4, stored proc里面尽量不要用dynamic statement,就是类似于:
CREATE PROCEDURE [dbo].[TradingPost_FindTradingCard]
-- Add the parameters for the stored procedure here
@username nvarchar(50),
@cardtype int,
@cardids nvarchar(max),
@buyoutcheck int,
@rvlevel int,
@startidx int,
@endidx int,
@noitem nvarchar(200)
WITH
EXECUTE AS CALLER
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- Insert statements for procedure here
DECLARE @sql nvarchar(max), @paramlist nvarchar(max);
SET @sql =
'with selecteditem as
(SELECT top (@endidx) ROW_NUMBER() OVER(order by tmi.buyout) rowidx,
tmi.id,
tmi.ownername,
tmi.biddername,
tmi.creationdate,
tmi.minbidprice,
tmi.buyout,
tmi.currentbidprice,
tmi.cardtype,
tmi.cardid,
tmi.lastbidtime,
tmi.quantity,
tmi.purchaser,
tmi.rank,
tmi.evopoint
FROM [tradingmarketitem] as tmi
where (datediff(mi,tmi.creationdate,getdate())) > 3 and (datediff(mi,tmi.creationdate,getdate())) < 2880 and tmi.ownername <> @username and tmi.cardtype = @cardtype and (datediff(mi,tmi.creationdate,getdate())) < 2880';
if @cardtype = 1 and @rvlevel <> 21
set @sql = @sql + ' and tmi.evlevel = @rvlevel';
if @buyoutcheck > 0
set @sql = @sql + ' and tmi.buyout > 0';
if @cardids <> 'null'
begin
set @sql = @sql + ' and tmi.cardid in (';
set @sql = @sql + @cardids;
set @sql = @sql + ')';
end
if @noitem <> 'null'
begin
set @sql = @sql + ' and tmi.id not in (';
set @sql = @sql + @noitem;
set @sql = @sql + ')';
end
set @sql = @sql +
')
select *from selecteditem where rowidx between (@startidx) and (@endidx);';
SET @paramlist = '@username NVARCHAR(50),@cardtype INT, @rvlevel INT, @startidx INT,@endidx INT';
print @sql
EXEC sp_executesql @sql, @paramlist,@username, @cardtype, @rvlevel,@startidx,@endidx;
END
5, raiserror severity 的疑惑
按理说 任何用户都可以指定 0 到 18 之间的严重级别。
[0,10]的闭区间内,不会跳到catch;
如果是[11,19],则跳到catch;
如果[20,无穷),则直接终止数据库连接;(来自http://www.cnblogs.com/xugang/archive/2011/04/09/2010216.html. 感谢)
但是不懂为什么用到16就会终止数据库连接,用到10,如果stored proc是个reader(有返还数据),则会去到catch,如果是个writer(execute non query),则没有任何反应(像没有Error一样继续执行程序),
6, 在sql里面可以用case,作用就像switch case
7, begin --- end 多用在多个statement的时候
8, 可以用dbname -> tasks -> Generate and Publish Scripts来存储或者复制一个表格,如果要连带表格里面的数据一起复制,记得在advanced option里面选择schema and data (Types of data to script)
先choose objects
然后
9, 为了避免deadlock, 在一些处理相同table的stored proc里面,尽量把处理的循序做到一致,这样会减少deadlock的发生.