This is what Joins are for. You don't have to get a list of patients ids and then send them back to the SQL engine. You can combine the two queries into one, with JOIN
:
SELECT x.*
FROM xrays AS x
JOIN appointments AS a
ON a.patID = x.patID
WHERE a.docID IN ('docid1', 'docid2', ..... , 'docidn')
ORDER BY x.patID --- optional so your application gets
--- the data of patients, ordered.
Other things to check is the indexes you have on the tables. As a rule of thumb, at least all columns used in joins, like your docID
, patID
, should have an index.
There are maybe several hundred writes a day, versus the tens of thousands of reads. My databases are located on an SSD drive.
Based on this statement, let's play with some numbers. Lets's say there are 500 writes a day and 20,000 reads a day. That computes out to the following
- 97.56% reads per day
- 2.44% writes per day
- 40 reads / write
As much as I love InnoDB, I would have to choose MyISAM in this case for several reasons
REASON #1
You only have 150MiB of MyISAM tables (70MiB Data, 80 MiB Indexes)
REASON #2
InnoDB indexes tend to get very bloaty because Secondary Indexes have keys into the Clustered Index. This always results in a double index lookup. This can be overlooked with large, write-heavy datasets.
REASON #3
InnoDB tablespaces tend to get very bloaty because of MVCC being created and discarded without an automatic reclaiming of disk space:
All this can be avoid with MyISAM
REASON #4
InnoDB protects individual rows by performing MVCC for transaction control. The overhead generated for reads in a day would probably be greater than 150 MiB.
I can probably name 2 or 3 more reasons, but let's cut to the chase: Is there anything that can improve performance for MyISAM in your case? Why, yes there is.
Your said the following
The table is about 260k rows in size, with 28 fields which for the most part is varchars and ints
If you have many varchars, there is something you can do to increase read/write performance. For any MyISAM table mydb.mytable
: run this command:
ALTER TABLE mydb.mytable ROW_FORMAT=Fixed;
What will this do, this will treat all VARCHARs as CHARs. Every row will be the exact same length. This will increase disk space 80%-100%. In your case, let's assume it doubles your 150 MiB MyISAM table to 300 MiB. Where is the benefit? Your MyISAM table can now be read/written anywhere from 20% - 30% faster without changing anything else I learned that from pages 72,73 from MySQL Database Design and Tuning.
I have written about this in the past:
Best Answer
You mentioned that the IN statement will include almost all possible values (90-95% based on your example numbers). Joe W is correct that depending on your setup it could go either way and it's impossible to say for sure without testing which of those two would be faster.
However, you will eventually run in to a diminishing return on a query like that. While one or the other will be faster than the other, neither will be fast. (by default MySQL will allow you up to max_allowed_packet for # of values, which is a lot). A faster option would be to use where NOT IN and just list the 5-10% of values you didn't want.