首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

,求简单SQL语句+优化

2012-03-13 
在线等,求简单SQL语句+优化。Create table log(logid int,tm datetime,mac varchar(255))Create table user

在线等,求简单SQL语句+优化。
Create table log(
logid int,
tm datetime,
mac varchar(255)
)

Create table userinfo(
ID int,
username,
userID int
)

create table usersouce(
userid int,
souce varchar(255)
)

log表记录用户的登陆记录,userinfo 表记录用户个人信息,usersocue 表记录用户来源,其中 log表的 logid=userinfo 表的ID,userinfo表的userid =usersouce表的userid , 各个表的数据量都在10W以上,其中logid有很多为0的记录,需要过滤下。希望大家帮忙写下最优化的连接查询。

[解决办法]

SQL code
select * from log a left join userinfo b on a.logid =b.userinfo left join usersouce c on b.userid =c.userid --然后给连接字段加上索引。--你的log表的主键是什么?
[解决办法]
SQL code
select * from [log] a left join userinfo b on a.logid =b.userinfo left join usersouce c on b.userid =c.userid -------a表的logid 字段加索引 ,b表的userinfo ,userid 字段加非聚集索引 ,c表的userid 加非聚集索引 

热点排行