r/SQLServer • u/chrisrdba • Feb 02 '26
Question Capture large queries of PII data?
Greetings. I want to capture all queries that return >= 500 records that contain PII data.
I can classify PII data, and I can of course use Extended Events or Profiler to capture queries limited to specific tables. I cannot figure out a way to combine the two only for larger queries.
Capturing any and all queries , even for just these specific tables seems like a bit much as many of them are used constantly.
Any ideas?
6
Upvotes
•
u/AutoModerator Feb 02 '26
After your question has been solved /u/chrisrdba, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/dbrownems Microsoft Employee Feb 02 '26
If you put too much logic into the Extended Event or Trace definition, you risk impacting performance more than just capturing all the queries and sifting through them later/elsewhere.
4
u/Tenzu9 Feb 02 '26
Why are you trying to catch it? why not just regulate access to it with user permissions and column encryption?
2
u/reditandfirgetit Feb 03 '26
Use SQL server audit . It's a lower impact than extended events . From my understanding it uses some kind of optimized for objects version of extended events. You can specify to audit selects on the objects with pii.
this is my limited understanding Write to files, more efficient
Use a queue delay . It will save the audit in memory then write to disk at the end of the delay. Delay is in ms time
Set the on_failure to continue sonit dues t cause issues if there is a failure
1
u/chrisrdba Feb 03 '26
Rather than answering each of these Im just going to reply here.
We currently dont do any auditing for PII usage and it's a request. It was requested that I capture queries that appear to be anomalies that touch all PII tables. Im pretty comfortable w both Extended Events and Profiler, but have always assumed more filtering in EE's was a good thing, not a bad thing, and that an unfiltered EE would kill my system (like Profiler).
Im well aware that PII can be siphoned off in batches smaller than 500, and know how to write a WHILE loop (thanks though!). I also assume that anyone trying to steal large amounts of data likely isnt going to do it 1 record at a time, an the # 500 was just something I picked randomly for this post.
If there's a good way to satisfy this request Im definitely open, thanks.
6
u/az987654 Feb 02 '26
Is this a homework problem or a real issue?
If it's a real issue and you're handling PII data this way, you should be terrified and look for employment elsewhere
1
u/7amitsingh7 Feb 04 '26
SQL Server doesn’t have a clean way to say “only capture queries that return >500 rows and contain PII.” You can use Extended Events or Audit to capture SELECTs, but filtering on rowcount + sensitive columns at capture time either isn’t possible or adds nasty overhead. Also, the assumption is flawed anyone can exfiltrate PII 50 rows at a time in a loop and never trip your threshold. If the real concern is who is accessing sensitive data, audit access to the specific tables/columns and analyze after the fact. Trying to catch “large queries” is security theater and you’ll still miss the bad stuff.