I want to create a relational database for financial data and struggling to figure out what the best approach to this is.
- 1st option: single table for all records with an index
- 2nd option: one table for each trading pair, but this feels incorrect because conceptually all records are the same (they represent candles)
I am wondering what the performance differences between the two would look like. My understanding is using a single table with an index should have the same performance than using multiple tables, because under the hood the database should create a tree that lets me quickly find data relative to a given pair. So the question is: how is an index (I believe b-tree
should be suited to this case) handled by the database at the low-level? Does it provide the same performance than separating data in multiple tables?
Now assuming the single-table approach and this schema
CREATE TABLE Candle (
Pair VARCHAR(127) NOT NULL,
OpenTime TIMESTAMP NOT NULL,
Open DECIMAL(20, 8) NOT NULL,
High DECIMAL(20, 8) NOT NULL,
Low DECIMAL(20, 8) NOT NULL,
Close DECIMAL(20, 8) NOT NULL,
BaseVolume DECIMAL(20, 8) NOT NULL,
QuoteVolume DECIMAL(20, 8) NOT NULL,
PRIMARY KEY (Pair, OpenTime),
INDEX(Pair, OpenTime)
);
The cardinality of Pair
is limited (a few hundreds values) whereas OpenTime
has hundreds of thousands of possible values (I want to store 1m candle data, so years of data would result in a lot of different values). Does the index on (Pair, OpenTime)
make sense or it's better to have an index on Pair
only? The idea between using both is I believe it should allow for faster queries that include sorting or filtering by timestamp. However, it should also increase the depth of my tree, although I don't know if the increase would be enough to cause noticeably slower queries. Please note that there are going to be billions of records in total.
Thanks in advance and let me know if I can improve my question in any way!
INDEX(Pair, OpenTime)
is excess as you havePRIMARY KEY (Pair, OpenTime)
.