• It still has a different top, different where, different select list and a different order to the OP's queries.

    Boris, try this, it should be a combination of your two. I did replace the database ID with the database name.

    Do note that the two DMVs return different numbers of rows, query stats returns a row per statement, cached plans a row per batch. Hence if you have procedures or batches with more than one statement, you'll see the same plan multiple times.

    SELECT DB_NAME(st.dbid) AS DatabaseName ,

    SUBSTRING(st.text, ( qs.statement_start_offset / 2 ) + 1, ( ( CASE qs.statement_end_offset

    WHEN -1 THEN DATALENGTH(st.text)

    ELSE qs.statement_end_offset

    END - qs.statement_start_offset ) / 2 ) + 1) AS sql_statement ,

    execution_count ,

    plan_generation_num ,

    last_execution_time ,

    total_worker_time ,

    last_worker_time ,

    min_worker_time ,

    max_worker_time ,

    total_physical_reads ,

    last_physical_reads ,

    min_physical_reads ,

    max_physical_reads ,

    total_logical_writes ,

    last_logical_writes ,

    min_logical_writes ,

    max_logical_writes ,

    UseCounts ,

    Cacheobjtype ,

    Objtype ,

    query_plan

    FROM sys.dm_exec_query_stats AS qs

    INNER JOIN sys.dm_exec_cached_plans AS cp ON qs.plan_handle = cp.plan_handle

    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st

    CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp

    WHERE st.objectid IS NULL

    ORDER BY qs.last_execution_time DESC

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass