English 中文(简体)
维持服务器数据库表的最大条目数目的最有效方式
原标题:Most efficient way to sustain a maximum number of entries in SQL Server database table

我很想知道,做以下工作的最有效方式是什么?

我试图实施某种审计制度,把我一页的每个记录储存在数据库中。 我使用了2005年服务器数据库。 储存审计数据的表格显然无法在没有上限的情况下增长。 因此,可以说,它应当拥有最多1,000个条目,然后在插入新的条目时,必须删除任何旧条目。 问题在于你如何以最有效的方式做到这一点——我是否需要增加任何特别栏目,如简明的入门号码,以便于清理?

EDIT: Say, if the structure of my table is (pseudo code):

`id` BIGINT autoincrement
`date` DATETIME
`data1` NVARCHAR(256)
`data2` INT

您将如何撰写这一清理程序?

最佳回答

正如托尼提到的那样,用日期来识别这些词语。 此外,在实地使用一个分组索引,以便插入总是在表尾,通过老行进行扫描和删除是容易和高效的。

如果你使用一些数据,那么就应该这样做:

DELETE FROM myTable WHERE someField < (SELECT MAX(someField) - 1000 FROM myTable)

删除一天以上的一切,就是:

DELETE FROM myTable WHERE someField < DateAdd( d , -1, getdate())
问题回答

日期不详。 检查一下你的情况,看看上去有多少天是/将。 删除比这更长的内容。 审计从来不特别有效,但如果你有数据,就会帮助那些效率极低的人。

如果我理解你的需要,那就应该工作。 我在2008年5月5日对它进行了测试,但我看不出为什么它不会在2005年服务器上工作。

  1. 采用标识触发器在审计表中插入一行。

  2. • 在你的审计桌上设立AFTER INSERT触发点,删除与MIN(ID)的行文。

Here s some code to play with:

∗ 编制审计表 *

CREATE TABLE ServerLogonHistory
(SystemUser VARCHAR(512),
ID BIGINT,
DBUser VARCHAR(512),
SPID INT,
LogonTime DATETIME)
GO

/* Create Logon Trigger */

CREATE TRIGGER Tr_ServerLogon
ON ALL SERVER FOR LOGON
AS
BEGIN
INSERT INTO TestDB.dbo.ServerLogonHistory
SELECT SYSTEM_USER, MAX(ID)+1 , USER,@@SPID,GETDATE()
FROM TestDB.dbo.ServerLogonHistory;
END
GO 

∗ 建立清理触发点 *

CREATE TRIGGER AfterLogin
ON TestDB.dbo.ServerLogonHistory
AFTER INSERT
AS
DELETE 
FROM TestDB.dbo.ServerLogonHistory 
WHERE ID = 
(SELECT MIN(ID) FROM TestDB.dbo.ServerLogonHistory);
GO;

A word of warning. If you create an invalid logon trigger, you ll not be able to logon to the database. But don t panic! It s all part of learning. You ll be able to use sqlcmd to drop the bad trigger.

我确实试图删除标识触发器中的min光灯,但我无法做到这一点。

1. “ID”一栏是1级的识别栏吗?

之后插入一行

delete page where id<IDENTability_CURRENT(YOUR_TABLE)-1000





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签