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

How replace text value of textbox in access 2007
Help Debugging this Code to Convert for .adp file type
Does Jet/ACE use indexes with a LIKE BeginsWith query?
Printing parameters in an Access parameter query
How do you set the required property when adding a column in Access using ADOX?
Export From Access to Excel
access 2010 - parameters button is disabled
Several If Statements in an access 2007 vba subroutine. Curious if there is another way?
reliable way of overwriting access database on live website
How to specify page width and length for printed (or html) report?
How to bind the contents of a combo box to the value of another
Access VBA: DLookUp function giving type mismatch error
Microsoft Access - If Null then
How do i reference the value of a control in a rowsource query?
How to query number of attachments from Attachment field in Microsoft Access?
Problem adding a row into a typed dataset

Categories

HOME
cakephp
arduino-uno
ionic-framework
pdf
openlayers
pypi
proxy
mfc
react-virtualized
programming-languages
c#-2.0
jpeg
datastax-java-driver
add
adfs
basic
gitpitch
vifm
kentor-authservices
dtrace
clearcase-ucm
systemc
foselasticabundle
ef-migrations
angular-ui
jqwidget
firefox-webextensions
nas
paging
sqlcipher
buildbot
fopen
neo4j-spatial
librato
tinymce-4
pdb
column-family
xmlreader
android-kernel
html5-fullscreen
git-merge
retina-display
abstract-class
theano.scan
reactive-cocoa-5
wptoolkit
qwt
upstart
aurelia-validation
orthogonal
color-picker
adxstudio-portals
phishing
underscore.js-templating
nsfilemanager
superstack
whois
rvest
design-by-contract
pervasive-sql
tcpserver
collapse
libressl
responsive-images
microbenchmark
event-bubbling
processmodel
nsight
camanjs
winddk
preferences
uitouch
contenttype
elliptic-curve
oracle-warehouse-builder
ember-charts
cdc
soundtouch
trusted
appconkit
code-cleanup
php-parser
xmemcached
android-sdk-2.1
fluent-interface
coda-slider
revisions
nintendo-ds
virtual-functions
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