这是一个有效且高效的SQL查询还是有更好的方法?

[英]Is this an effective and efficient SQL Query or is there a better way?


We have a passwords table which references a user table. Records never get deleted from the password table so if a user changes their password, a new entry with a more recent Created date gets inserted.

我们有一个引用用户表的密码表。记录永远不会从密码表中删除,因此如果用户更改其密码,则会插入具有更新创建日期的新条目。

The hash of the password is salted with various things, most importantly the created date of the actual record.

密码的哈希值受各种事物的影响,最重要的是实际记录的创建日期。

In a stored procedure, I'm retrieving variables so I can do a hash for comparison. I really just want to store the most recent password hash for a user along with the record's created date:

在存储过程中,我正在检索变量,因此我可以进行哈希比较。我真的只想为用户存储最新的密码哈希以及记录的创建日期:

DECLARE @ExistingPassword as varchar(200)
DECLARE @LastChanged as DateTime

SELECT Top 1
    @ExistingPassword = p.PasswordHash,
    @LastChanged = p.Created, 
FROM Password as p
WHERE p.UserId = @UserId
ORDER BY p.Created DESC

Is this a reasonably efficient way of getting the most recent password hash and its created date? Is there a better way to do it?

这是获取最新密码哈希及其创建日期的合理有效方式吗?有没有更好的方法呢?

3 个解决方案

#1


2  

This is efficient, but I would make sure there is an index on:

这是有效的,但我会确保有一个索引:

userId and created 

for performance reasons

出于性能原因

#2


1  

I think that is the fastest way to do it. I ran the below code to test your way and the only other way I could think of and your way was a little faster. I would also like to mention in a table with 1,000,000 rows I was getting response times fast enough that SSMS was not even giving me a run time, just 00:00:00. I did see one thing that might help you. If you can, put a non-clustered index on the Created column and make it sort in Descending order. Putting it in Descending order will make big difference because it will stop you from doing a table scan for the newest Created DateTime. That really could make a HUGE difference.

我认为这是最快的方法。我运行下面的代码测试你的方式和我能想到的唯一的其他方式,你的方式更快一点。我还想在一个包含1,000,000行的表中提到我的响应时间足够快,以至于SSMS甚至没有给我一个运行时间,只是00:00:00。我确实看到了一件可能对你有帮助的事情。如果可以,请在“已创建”列上放置非聚集索引,并使其按降序排序。将它按降序排列会产生很大的不同,因为它会阻止您对最新创建的DateTime进行表扫描。这真的可以产生巨大的变化。

--Put Rows in TestTable, 3 Rows ID, User_ID, CreatedTime
DECLARE @Count INT
SET @Count = 1

WHILE @Count <= 1000000
BEGIN

INSERT INTO TestTable
VALUES (@Count%3+1,DATEADD(HH,@Count,getdate()))

SET @Count = @Count + 1

END

--Select Your way
SELECT TOP 1
 User_ID
FROM TestTable
WHERE User_ID = 3
ORDER BY CreatedTime DESC

--Select my way
SELECT
 User_ID
FROM TestTable
WHERE User_ID = 3
 and CreatedTime = (SELECT MAX(CreatedTime) FROM TestTable WHERE User_ID = 3)

#3


0  

By my personal experience, you should work with a period, like StartDate and EndDate. So, you can to use a WHERE getdate() BETWEEN StartDate and EndDate to search for your current password.

根据我的个人经验,你应该使用一段时间,比如StartDate和EndDate。因此,您可以使用WHERE getdate()BETWEEN StartDate和EndDate来搜索当前密码。

Every time you add a record just with a created date for subsequent most update retrieval, baby jesus cries.

每次你添加一个创建日期的记录,以便随后进行大多数更新检索,小耶稣会哭。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/01/15/a52539d98350cab9ce7cea6787f3bf7f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告