ms-access


Very slow subqueries when using “NOT IN”


I'm working on generating reports for data contained within a large pre-existing Access database (~500 mb after compact & repair), and I'm having trouble with a slow subquery.
The database has a big table which contains a record of every customer purchase. Here's a simple query which finds customers who have bought a blue widget. It completes within a few seconds and returns about ten thousand records.
SELECT DISTINCT CustomerId
FROM ProductSales
WHERE Product = 'BLUE'
Here's a query which tries to find customers who have bought a blue widget, but not a red widget. It takes about an hour to run.
SELECT DISTINCT CustomerId FROM ProductSales
WHERE Product = 'BLUE'
AND CustomerId NOT IN (
SELECT CustomerId
FROM ProductSales
WHERE Product = 'RED'
)
Is there a way to refactor the second query to make it take a few minutes instead of an hour?
Access' database engine can't use an index for Not In, so it's bound to be slow. With an index on CustomerId, this query should be much faster because the db engine can use the index.
SELECT DISTINCT blue.CustomerId
FROM
ProductSales AS blue
LEFT JOIN
(
SELECT CustomerId
FROM ProductSales
WHERE Product = 'RED'
) AS red
ON blue.CustomerId = red.CustomerId
WHERE
blue.Product = 'BLUE'
AND red.CustomerId Is Null;
You could probably also try a Not Exists approach, but index use there is not guaranteed. Also, please see the comment below from David Fenton which discusses performance impact in more detail.
Add an index, of course, if you don't have one. If that's a problem, it's probably just that there are lots of customers with orders for something other than RED but not so many with BLUE; this (untested) query tries to fix that.
SELECT DISTINCT CustomerId FROM ProductSales
LEFT JOIN (
SELECT DISTINCT CustomerId cid FROM ProductSales
LEFT JOIN (
SELECT DISTINCT CustomerId
FROM ProductSales
WHERE Product = 'BLUE'
) foo ON CustomerId = cid
WHERE Product = 'RED'
) bar USING (CustomerId)
WHERE cid IS NULL

Related Links

Adding Pages to an Access Tab Control using VBA
Build VBA Sql statement using 3 multi-select Listboxes and a date range in MS Access 2016 32 bit
ms-access remove fails on the same date from the same person when there is a pass
Access 2016 vba to send email based on query results and record date email sent back to same table
Count unique values in Access
If ComboBox on Form is blank, Then
Trying to do a calculation with data from a date and the data from the day before
MS Access Front-end: Does each user need their own copy?
trying to filter subform based on contents of field on main form
How to use MS access on a server where MS Office is not installed
How do i import and eliminate first two row of website in msaccess while importing html pages
Multi criteria dlookup issue
Count all checkboxes in ms-access form…what am I doing wrong?
How do I set a default selection to a Listbox in MS Access
Microsoft Access not overwriting when exporting a query to excel
Using ms-access command-line tag /x but database opens read-only

Categories

HOME
sendgrid
pdf
client
testng
comparison
plot
stock
dot
jsrender
electronics
twitter-bootstrap-4
binary-tree
sql-server-2016
convolution
mouse
node-notifier
medical
dacpac
google-translate
maude-system
communication
fancybox-3
numeral.js
moonmail
windows-server-2012
ups
designer
libtiff
seaborn
fatal-error
connection-string
autoconf
opencover
jasonette
arabic
wkwebview
crystal-reports-2010
javacv
applozic
spring-mybatis
log4js-node
wtx
react-chartjs
key-value-observing
twilio-api
catch-all
devextreme
adobe-premiere
streamsets
dism
fedex
botbuilder
eigenvalue
vao
netcdf4
skeleton-css-boilerplate
logfiles
avconv
filepicker
prolog-setof
ionicons
tactic
android-cursor
or-tools
pycaffe
cubes
medium.com
gridpane
url-masking
py2app
google-web-starter-kit
clang-static-analyzer
flash-cs5
php-internals
revolution-r
teamcity-8.0
google-places
wordml
purge
iis-arr
ibaction
system32
bridge.net
jsapi
pick
gulp-less
neolane
flask-cors
navigationservice
kgdb
mechanize-ruby
cos
html4
eventual-consistency
html-editor
free-variable
typoscript2
pvrtc
getmessage
enter
transactionscope
gnu-prolog
qt-jambi
subgurim-maps
tomcat-valve
table-footer
celltable
sortable-tables
nintendo-ds
adrotator
procedural-music
mediarss

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App