Monday, December 23, 2019

The query processor ran out of internal resources and could not produce a query plan. Error 8623 Severity 16 State 1


The query processor ran out of internal resources and could not produce a query plan, Error 8623 Severity 16 State 1

Error: 8623, Severity: 16, State: 1.
"The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information."




We were getting the above error very frequently in one of my production environments. The query was coming from application servers, so we were not able to pin point the query. The SQL also does not capture the failed queries by default.

Microsoft says that there may be couple of reason if your query fails with this error. As we were not having the original query, so it was difficult for us to proceed.

One of the obvious reasons is that a Complex query or a large number of tables used as a query reference-

Complex queries specially with many entries in the "IN" clause (> 10,000).

I searched on google and found that we can capture such queries by using extended event.
You can find the script on below link of Brent Ozar website:


I used below script to create extended event and finally I was able to find the problematic query.

Script to create extended Event: You may need to change the directory file in your case

CREATE EVENT SESSION [FailedQueries] ON SERVER
ADD EVENT sqlserver.error_reported
    (ACTION(sqlserver.client_app_name, sqlserver.client_hostname, 
        sqlserver.database_name, sqlserver.sql_text, sqlserver.username)
    WHERE ([severity]> 15))
 ADD TARGET package0.event_file (SET
    filename = N'B:\Test\XEvents\FailedQueries.xel'
    ,metadatafile = N'B:\Test\XEvents\FailedQueries.xem'
    ,max_file_size = (5)
    ,max_rollover_files = (10))
 WITH (STARTUP_STATE = ON)
GO


Use below query to see the logs:

SELECT
    [XML Data],
    [XML Data].value('(/event[@name=''error_reported'']/@timestamp)[1]','DATETIME')             AS [Timestamp],
    [XML Data].value('(/event/action[@name=''database_name'']/value)[1]','varchar(max)')        AS [Database],
    [XML Data].value('(/event/data[@name=''message'']/value)[1]','varchar(max)')                AS [Message],
    [XML Data].value('(/event/action[@name=''sql_text'']/value)[1]','nvarchar(max)')             AS [Statement]
FROM     (SELECT         OBJECT_NAME              AS [Event],
        CONVERT(XML, event_data) AS [XML Data]
    FROM         sys.fn_xe_file_target_read_file
    ('B:\Test\XEvents\FailedQueries*.xel',NULL,NULL,NULL)) as FailedQueries
                    where [XML Data].value('(/event/action[@name=''sql_text'']/value)[1]','varchar(max)')  is not null
                    and  [XML Data].value('(/event/data[@name=''message'']/value)[1]','varchar(max)')  like '%The query processor ran out of internal resources and could not produce a query plan %' ;
GO



The query which we found in our case was having a lot of values (>10000) inside IN Clause also the query was running with legacy optimizer on SQL server 2016.

In order to test the query, I tried to run with hint OPTION( QUERYTRACEON 2312) but was still failing with same error. This hint allows SQL server to execute the query with help of new query optimizer (>SQL2012).

And finally I tried to run the same query with help of hint OPTION(FORCE ORDER) and the query completed without any issue.


There are some other workaround to as per Microsoft which are as below:

You could try to run the query using the hint option (force order), option (hash join), option (merge join), option (querytraceon 4102)  with a plan guide. By enabling the traceflag 4102, we will revert the behavior to SQL Server 2000 for handling semi-joins.

Please share if there was any other issue in your case.






Friday, December 13, 2019

"syspolicy_purge_history" SQL Agent Job is failing with error "Cannot find drive A drive with the name 'SQLSERVER' does not exist"


The SQL Agent Job "syspolicy_purge_history" was failing in our environment since we have upgraded our SQL server instance from SQL 2008R2 to SQL2012. The job was failing at step-3 named as "Erase Phantom System Health Records" We have tried each and every article available over internet, but nothing was helpful in our case. After a lot of troubleshooting my friend Desh found the solution for it. 

There are still lot of DBA's searching the fix of same issue. So, I want to share the all steps which fixed the issue. You can try it in your case and let us know if this works.

Issue Details:

SQL Server version: SQL Server 2012
Job Name: syspolicy_purge_history

Error:
Executed as user: "XXXXX". The job script encountered the following errors. 
These errors did not stop the script:  A job step received an error at line 1 in a PowerShell script. The corresponding line is 'import-module SQLPS  -DisableNameChecking'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'The specified module 'SQLPS' was not loaded because no valid module file was found in any module directory.  '  A job step received an error at line 2 in a PowerShell script. The corresponding line is '(Get-Item SQLSERVER:\SQLPolicy\ServerName$a).EraseSystemHealthPhantomRecords()'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find drive. A drive with the name 'SQLSERVER' does not exist.  '  A job step received an error at line 2 in a PowerShell script. The corresponding line is '(Get-Item SQLSERVER:\SQLPolicy\



Steps to Fix the issue:

The first step is to check if SQLPS folder exists to below location:

C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules




Now Open Computer Properties and then click on advance system settings> environment variables> Copy the environment path to a notepad file




%ProgramFiles%\WindowsPowerShell\Modules;
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules

Now copy the SQLPS folder from location “C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules“ to below locations which we found under environment variables:

 %ProgramFiles%\WindowsPowerShell\Modules
                                           %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules

You can Open the above two locations via start>run> and then paste location and then Enter.
Once this folder is copied to these locations. Try to run the below command from Powershell.

Open Powershell  as admin and then execute the below command.
import-module SQLPS  -DisableNameChecking


Once import module is done. Open the Group Policy Object Editor as below:
  1.  Start > run > "mmc"
  2.  File > Add/Remove Snap-In...
  3.  Under Available snap-ins, select "Group Policy Object Editor" and click Add, then Finish.
  4.  Click OK to snap-in the GPO Editor

Now navigate to the specific policy:
  1.  Console Root > Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > Windows Powershell
  2.  Open-up the "Turn on Script Execution" policy setting
  3.  Enable Script Execution and in Options select “ Allow All Scripts”. Apply the setting and click on OK.
  4.  Exit from this window

Below is the screen shot for above steps for your reference:









Exit from this window and during save pop-up dialog click on No.


Now Try to run the Job it should be running without any issue.




Hope this works in your case.

Sunday, September 1, 2019

Memtable and SStable In Cassandra


What are Memtable and SStable In Cassandra?

Cassandra processes data at several stages on the write path, starting with the immediate logging of a write and ending in with a write of data to disk:
  • 1.     Logging data in the commit log
  • 2.     Writing data to the memtable
  • 3.     Flushing data from the memtable
  • 4.     Storing data on disk in SSTables

 memtable

When a write occurs, Cassandra stores the data in a memory structure called memtable, and to provide configurable durability, it also appends writes to the commit log on disk. The commit log receives every write made to a Cassandra node, and these durable writes survive permanently even if power fails on a node. The memtable is a write-back cache of data partitions that Cassandra looks up by key. The memtable stores writes in sorted order until reaching a configurable limit, and then is flushed.

When memtable contents exceed a configurable threshold or the commitlog space exceeds the commitlog_total_space_in_mb, the memtable data, which includes indexes, is put in a queue to be flushed to disk. To flush the data, Cassandra sorts memtables by partition key and then writes the data to disk sequentially. The process is extremely fast because it involves only a commitlog append and the sequential write.

Data in the commit log is purged after its corresponding data in the memtable is flushed to the SSTable. The commit log is for recovering the data in memtable in the event of a hardware failure.

 SSTable:

A sorted string table (SSTable) is an immutable data file to which Cassandra writes a memtable. Cassandra flushes all the data in the memtables to the SSTables once the memtables reach a threshold value. Consequently, a partition is typically stored across multiple SSTable files. A number of other SSTable structures exist to assist read operations:
For each SSTable, Cassandra creates these structures:
Data (Data.db) 
The SSTable data
Primary Index (Index.db) 
Index of the row keys with pointers to their positions in the data file
Bloom filter (Filter.db) 
A structure stored in memory that checks if row data exists in the memtable before accessing SSTables on disk
Compression Information (CompressionInfo.db) 
A file holding information about uncompressed data length, chunk offsets and other compression information
Statistics (Statistics.db) 
Statistical metadata about the content of the SSTable
Digest (Digest.crc32, Digest.adler32, Digest.sha1) 
A file holding adler32 checksum of the data file
CRC (CRC.db) 
A file holding the CRC32 for chunks in an uncompressed file.
SSTable Index Summary (SUMMARY.db) 
A sample of the partition index stored in memory
SSTable Table of Contents (TOC.txt) 
A file that stores the list of all components for the SSTable TOC
Secondary Index (SI_.*.db) 
Built-in secondary index. Multiple SIs may exist per SSTable

References: Datastax Docs


Bloom Filters In Cassandra


In General, A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. False positive matches are possible, but false negatives are not – in other words, a query returns either "possibly in set" or "definitely not in set".

In Cassandra, Bloom filters are used to boost the performance of reads. It is non-deterministic algorithms for testing whether an element is a member of a set. They are non-deterministic because it is possible to get a false-positive read from a Bloom filter, but not a false-negative.

Bloom filters work by mapping the values in a data set into a bit array and condensing a larger data set into a digest string using a hash function. The digest, by definition, uses a much smaller amount of memory than the original data would. The filters are stored in memory and are used to improve performance by reducing the need for disk access on key look-ups. Disk access is typically much slower than memory access. So, in a way, a Bloom filter is a special kind of cache.

When a query is performed, the Bloom filter is checked first before accessing disk. Because false-negatives are not possible, if the filter indicates that the element does not exist in the set, it certainly doesn’t; but if the filter thinks that the element is in the set, the disk is accessed to make sure.

  
Bloom filters are implemented by the org.apache.cassandra.utils.BloomFilter
class. Cassandra provides the ability to increase Bloom filter accuracy (reducing the number of false positives) by increasing the filter size, at the cost of more memory. This false positive chance is tuneable per table.


An example of a Bloom filter, representing the set {x, y, z}. The colored arrows show the positions in the bit array that each set element is mapped to. The element w is not in the set {x, y, z}, because it hashes to one bit-array position containing 0. For this figure, m = 18 and k = 3.




References:  wikipedia, Cassandra The Definitive Guide

SQL Server 2016 Database Mail Is Not Working


If you are working on SQL Server 2016 and your machine is having .NET Framework higher than 3.5 then you will not be able to send using DB email.


As Per Microsoft Support, there's a bug in SQL server 2016 Setup that causes the database mail not to work without .net 3.5. So they provided a workaround for this bug  by creating a DatabaseMail.exe.config file. The steps to create this file are as below



Create the DatabaseMail.exe.config and drop it next to the DatabaseMail.exe under the Binn folder. You can use notepad.exe or any other editor to edit it. Just make sure you save it by using UTF-8 encoding (in notepad.exe, select Save As... and in the Encoding combo box, select UTF-8): 

         <?xml version="1.0" encoding="utf-8" ?>
         <configuration>
         <startup useLegacyV2RuntimeActivationPolicy="true"> 
         <supportedRuntime version="v4.0"/>     
         <supportedRuntime version="v2.0.50727"/>
         </startup>
         </configuration>


Note:
Sometimes I have observed that the config file get disappear automatically after reboot. So, to fix that you can create a SQL agent job which will create/copy this file (DatabaseMail.exe.config ) in Binn folder and schedule to run it after every service reboot. This way you can overcome with this issue.




SQL Script to find Last Statistics update date for a Table


You can find below query useful when you are trying to see the last stats update date of few selected tables in SQL Server.

Query:

SELECT
    sch.name + '.' + so.name AS 'Table',
    ss.name AS 'Statistic',
      CASE
            WHEN ss.auto_Created = 0 AND ss.user_created = 0 THEN 'Index Statistic'
            WHEN ss.auto_created = 0 AND ss.user_created = 1 THEN 'User Created'
            WHEN ss.auto_created = 1 AND ss.user_created = 0 THEN 'Auto Created'
            WHEN ss.AUTO_created = 1 AND ss.user_created = 1 THEN 'Not Possible?'
      END AS
'Statistic Type',
    CASE
            WHEN ss.has_filter = 1 THEN 'Filtered Index'
            WHEN ss.has_filter = 0 THEN 'No Filter'
      END AS
'Filtered?' ,
    sp.last_updated AS 'Stats Last Updated',     sp.rows AS 'Rows',
    sp.rows_sampled AS
'Rows Sampled',
    sp.unfiltered_rows AS
'Unfiltered Rows',
      sp.modification_counter AS
'Row Modifications',
      sp.steps AS
'Histogram Steps'
FROM sys.stats ss JOIN sys.objects so ON ss.object_id = so.object_id
JOIN sys.schemas sch ON so.schema_id = sch.schema_id
OUTER APPLY sys.dm_db_stats_properties(so.object_id, ss.stats_id) AS sp WHERE so.TYPE = 'U' 
and so.name in('Your_Table_Name') --Enter Your table name here

ORDER BY sp.last_updated DESC;


Saturday, August 31, 2019

SQL Query to Find Excessive Memory Grant

Sometime we need to check the memory used by current running query on a SQL instance.
Below is the query which I use during performance troubleshooting and it really helps me many a times. The query will provide you the current execution plan along with the memory granted, memory_used and memory requested by the query.

Query:

SELECT mg.granted_memory_kb, mg.session_id, t.text,mg.requested_memory_kb,mg.used_memory_kb,mg.query_cost, qp.query_plan,mg.plan_handle,mg.sql_handleFROM sys.dm_exec_query_memory_grants AS mgCROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) AS tCROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) AS qpORDER BY 1 DESC OPTION (MAXDOP 1)



Hope you will find this useful :)