1.对于CROSS APPLY 和 OUTER APPLY 的应用:
CROSS APPLY 类似于INNER JOIN 但是,可以规定对于满足条件的数据需要关联几行,应用场景: 每个零件把第一个工单关联出来;
OUTER APPLY 类似于LEFT JOIN ,不同之处同上;
--inner join
SELECT a.part_no,b.workorder FROM dbo.sys_part a INNER JOIN dbo.g_workorder_detail b ON a.part_id =b.part_id
---执行结果:
part_no workorder
1144514
1912900
1912900
1912900
1912900
1912900
1144514
---cross apply
SELECT a.part_no,c.workorder FROM dbo.sys_part a CROSS APPLY
(SELECT TOP 1 * FROM dbo.g_workorder_detail b WHERE a.part_id =b.part_id ORDER BY b.workorder_id) AS c
执行结果:
part_no workorder
1144514
1912900
---outer apply
SELECT a.part_no,c.workorder FROM sys_part a OUTER APPLY
(SELECT TOP 1 * FROM dbo.g_workorder_detail b WHERE a.part_id =b.part_id ORDER BY b.workorder_id) AS c
---执行结果:
part_no workorder
90740 NULL
90789 NULL
369832 NULL
507994 NULL
2.SQL语句执行结果增加列显示:
--其中对于over之后的排序为整个语句显示信息的排序
SELECT TOP 5 ROW_NUMBER() OVER(ORDER BY part_id) AS row_id ,part_no FROM sys_part
3.SQL语句对于锁表的查询:
SELECT b.name AS [database], a.spid, a.blocked, a.waittime / 1000 AS wait_time, a.loginame,CONVERT(VARCHAR(50),login_time,120) login_time,
CONVERT(VARCHAR(50),last_batch,120) last_batch, open_tran, a.status, hostname, program_name, cmd
FROM sys.sysprocesses a
JOIN sys.sysdatabases b ON a.dbid = b.dbid
WHERE spid IN ( SELECT blocked
FROM sys.sysprocesses
WHERE blocked > 0 AND spid != blocked )
UNION
SELECT b.name AS [database], a.spid, a.blocked, a.waittime / 1000 AS wait_time,a.loginame, CONVERT(VARCHAR(50),login_time,120) login_time,
CONVERT(VARCHAR(50),last_batch,120) last_batch, open_tran, a.status, hostname, program_name, cmd
FROM sys.sysprocesses a
JOIN sys.sysdatabases b ON a.dbid = b.dbid
WHERE blocked > 0 AND waittime >0 AND spid != blocked
ORDER BY spid
--对于得到的具体spid可以根据语句来得到具体的sql语句: DBCC INPUTBUFFER(SPID)
4.SQL中对于登录信息的获取:
SELECT
s.[host_name],
con.client_net_address,
s.login_name,
s.[program_name]
FROM sys.dm_exec_connections AS con INNER JOIN sys.dm_exec_sessions AS s
ON con.session_id=s.session_id AND s.session_id=@@SPID ---当前执行此内容的SPID
5.对于表碎片的查看,决定是否要重建索引:
DBCC SHOWCONTIG(sys_part)
---显示结果:
DBCC SHOWCONTIG scanning 'sys_part' table...
Table: 'sys_part' (308912172); index ID: 0, database ID: 46
TABLE level scan performed.
- Pages Scanned................................: 276
- Extents Scanned..............................: 39
- Extent Switches..............................: 38
- Avg. Pages per Extent........................: 7.1
- Scan Density [Best Count:Actual Count].......: 89.74% [35:39] ---此百分比越大越好
- Extent Scan Fragmentation ...................: 33.33% ---扩展盘区扫描碎片,越小越好
- Avg. Bytes Free per Page.....................: 2542.0
- Avg. Page Density (full).....................: 68.59% ------------每叶的密度 --此值太低,就得考虑索引重建
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
---如需要重建索引,可执行: DBCC DBREINDEX(sys_part)
6.对于JOB 方面的查询,归属,及其执行的内容
---对于Job是归属那个账号 在执行的查询
SELECT a.name AS job_name,b.loginname,a.date_created AS job_createtime,a.date_modified AS jobmodifytime
FROM msdb.dbo.sysjobs a INNER JOIN master.dbo.syslogins b ON a.owner_sid =SUSER_SID(B.loginname)
WHERE 1=1
AND A.enabled =1 --表示启用的JOB
AND B.loginname='sa' --账号
ORDER BY a.job_id
--关于在运行中的job执行的语句
SELECT a.name AS job_name,b.step_name,b.database_name,b.step_id,b.command
FROM msdb.dbo.sysjobs a INNER JOIN msdb.dbo.sysjobsteps b ON a.job_id=b.job_id
WHERE 1=1
AND a.enabled =1
ORDER BY a.job_id,b.step_id