2009-04-02 40 views

Trả lời

0

Không dễ dàng, nhưng nếu bạn bật tính năng ghi nhật ký của SQL Server, bạn có thể kiểm tra nhật ký bằng phần mềm để khám phá khi truy vấn cuối cùng là gì và nó là gì.

-Adam

1

Bạn cần có kiểm toán, hay một dấu vết sql server thiết lập trước thời hạn.

0

Máy chủ SQL có thể ghi nhật ký thông tin sự kiện cho các lần đăng nhập và bạn có thể xem nó bằng cách xem lại báo lỗi. Bằng cách bật cấp độ kiểm tra của SQL Server.

làm theo các bước sau để kích hoạt tính năng kiểm toán của tất cả/kết nối thành công với Enterprise Manager trong SQL Server:

Mở rộng một nhóm máy chủ. Bấm chuột phải vào máy chủ, rồi bấm Thuộc tính. Trên tab Bảo mật, trong Cấp độ kiểm tra, hãy nhấp vào tất cả/thành công, v.v. (yêu cầu tùy chọn).

Bạn phải dừng lại và khởi động lại máy chủ để thiết lập này có hiệu lực thi hành

16
SELECT 
last_user_seek = MAX(last_user_seek), 
last_user_scan = MAX(last_user_scan), 
last_user_lookup = MAX(last_user_lookup), 
last_user_update = MAX(last_user_update) 
FROM 
sys.dm_db_index_usage_stats 
WHERE 
[database_id] = DB_ID() 

Một caveat với phương pháp này là các thông tin trong DMV sẽ bị xóa và nulled bất cứ khi nào bạn khởi động lại SQL Server.

4

Tìm thấy điều này tại số MySQLtips - đã làm việc cho tôi.

select d.name, x1 = 
(select X1= max(bb.xx) 
from (
    select xx = max(last_user_seek) 
     where max(last_user_seek) is not null 
    union all 
    select xx = max(last_user_scan) 
     where max(last_user_scan) is not null 
    union all 
    select xx = max(last_user_lookup) 
     where max(last_user_lookup) is not null 
    union all 
     select xx = max(last_user_update) 
     where max(last_user_update) is not null) bb) 
FROM master.dbo.sysdatabases d 
left outer join 
sys.dm_db_index_usage_stats s 
on d.dbid= s.database_id 
group by d.name 
4

Để mở rộng về câu trả lời của James Allen:

SELECT d.name, 
last_user_seek = MAX(last_user_seek), 
last_user_scan = MAX(last_user_scan), 
last_user_lookup = MAX(last_user_lookup), 
last_user_update = MAX(last_user_update) 
FROM sys.dm_db_index_usage_stats AS i 
JOIN sys.databases AS d ON i.database_id=d.database_id 
GROUP BY d.name 

Sử dụng phiên bản sửa đổi này nếu bạn không muốn kết quả của mỗi bối cảnh cơ sở dữ liệu và muốn bao gồm tên cơ sở dữ liệu ở phần đầu của tập kết quả.

+0

Bạn cũng có thể thực hiện việc này mà không cần JOIN: SELECT DB_NAME (database_id), last_user_seek = MAX (last_user_seek), last_user_scan = MAX (last_user_scan), last_user_lookup = MAX (last_user_lookup), last_user_update = MAX (last_user_update) TỪ sys.dm_db_index_usage_stats GROUP BY database_id ĐẶT HÀNG BỞI DB_NAME (database_id) –

0

Tương tự như phương pháp trên, nhưng để có được kết quả đơn giản là một giá trị duy nhất cho một id cơ sở dữ liệu cụ thể (trong ví dụ này nó 6)

select max(LastAccess) 
from (
    SELECT last_user_seek as LastAccess FROM sys.dm_db_index_usage_stats WHERE last_user_seek is not null and [database_id]=6 
    union 
    SELECT last_user_lookup as LastAccess FROM sys.dm_db_index_usage_stats WHERE last_user_seek is not null and [database_id]=6 
    union 
    SELECT last_user_seek as LastAccess FROM sys.dm_db_index_usage_stats WHERE last_user_seek is not null and [database_id]=6 
    union 
    SELECT last_user_update as LastAccess FROM sys.dm_db_index_usage_stats WHERE last_user_seek is not null and [database_id]=6 
) UserAccess 
Các vấn đề liên quan