我有这个查询
select count(distinct user1_.Id) as col_0_0_ from [FriendCloseness] friendclos0_ inner join [User] user1_ on friendclos0_.User_id=user1_.Id where dateadd(d, 0, datediff(d, 0, friendclos0_.LastUpdate))=@p0 and friendclos0_.IsInvited=1 and user1_.IsDeleted=0 and user1_.Sex=@p1
表用户加入FriendCloseness 使用FriendClossness字段查询LastUpdate,IsInvited, 用户字段IsDeleted,性别在哪里
表涉及查询的用户结构
用户 ID,(主键) 请将isDeleted, 性别,
FriendClossness Id,(主键,DBMS会自动将索引设置为主键吗?) LastUpdate(datetimeoffset), IsInvited, User_id(外键)
我 wounder哪个字段需要索引,我想删除日期功能,方法是创建新字段“InviteDate”以仅存储日期
答案 0 :(得分:1)
试试这个 -
SELECT col_0_0_ = COUNT(DISTINCT u.Id)
FROM (
SELECT u.Id
FROM dbo.[User] u
WHERE u.IsDeleted = 0
AND u.Sex = @p1
--GROUP BY u.Id
) u
JOIN (
SELECT
f.[user_id]
, LastUpdate = DATEADD(DAY, 0, DATEDIFF(DAY, 0, f.LastUpdate))
FROM dbo.[FriendCloseness] f
WHERE f.IsInvited = 1
) f ON f.[user_id] = u.Id
WHERE f.LastUpdate = @p0