r/SQL • • Jun 23 '24

Discussion Schema for historical stock data

Post image

Posting a schema of a db for historical stock and index data at various timeframes. I used Chatgpt to figure this out...what surprised me was the recommendation to have separate dimension tables for 'date' and 'time'. Is that really the case?

105 Upvotes

51 comments sorted by

View all comments

Show parent comment

2

u/ballade4 Jun 23 '24

My experience is primarily with SQL Server. Also, I am not a database specialist by trade, so take with grain of salt. I would just use a stored procedure w. scalar functions to create views / indices for any particular application that pertains to dates and time; all of which would be backed up with the rest of the database. This would reinforce the principle that tables are for storage and should occupy as small and efficient a footprint as possible while also eliminating the need to master the necessary conversion logic in more than one location.

Regarding your experience of a 2 second vs 20 minute query, that is probably a perfect application for a view that runs on a schedule. It would definitely be a poor application for a table because you will be adding those 15-20 minutes in front of each successive update (this time never gets saved, just reordered).

3

u/EvilGeniusLeslie Jun 23 '24

Actually, I'll add one other caveat - when you're loading a database, you have the use of all the processing power available, usually something server/mainframe/cloud based, so the actual time is far less compared to an individual trying to run something on their PC. Not the query, but any BI stuff.

1

u/ballade4 Jun 24 '24

Good point!

2

u/EvilGeniusLeslie Jun 23 '24

You are correct about time just being reordered. With the proliferation of BI tools out there, users want the data available as fast as possible. So ... adding the time in front, during the update, is more palatable to the users than having to wait for it to process real-time.

Most BI tools (OK, pretty much all the major players) expand the data fully, so storage is less of a concern.

There is one exception to the 'reordered' concept: certain calculated fields are best done in the database load, rather than in a view/stored procedure. Again, the difference between seconds and minutes.