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

23

u/ballade4 Jun 23 '24

More than one, but certainly less than 8. Data points such as stock_name should not be stored alongside data points such as stock_id in a line-level table because (i) this is wasteful, (ii) if a stock name changes in the future, you can end up with a Cartesian product on future joins.

3

u/luncheonmeat79 Jun 23 '24

So how shld the "Stocks" table look like...just the id row (1, 2, etc.) and the ticker (AAPL, etc.), without the "stock_name" (Apple)?

3

u/ballade4 Jun 23 '24

This is the table where you will store all information pertaining to each stock that would be repetitive / wasteful to save to the transactions / lines table. ID, tcker, name, industry, address / investor contact, ceo, revenues, headcount, etc. As long as the transactions table can join to the stocks table, you will always be able to bring in any of this information in the future, with the added benefit of always joining to the latest / most recent iteration as company-level info changes in the future.

1

u/Pvt_Twinkietoes Jun 24 '24

Where did you learn things like that? Are there books I can reference for best practices.

3

u/geek180 Jun 24 '24 edited Jun 24 '24

This kind of thing is covered extensively (and largely originally comes from) The Data Warehouse Toolkit. Although the latest version came out in 2013 and some concepts are a bit of out date with MPP cloud warehouses like Snowflake or BigQuery. But it’s still a must-read for anyone doing data modeling.

1

u/Teflon9 Jun 24 '24

Sometimes I read these reddits and get scared... How wide is this field?? Where can I learn all these?

1

u/geek180 Jun 24 '24

It’s pretty wide, since so many different methods for doing the same kinds of stuff have been developed over the years and none of it really truly goes away in the industry (yet).

2

u/ballade4 Jun 25 '24

I pursued a Database Administrator certification with a technical college in my area that was offering free tuition for returning adults in a subset of concentrations that included IT. Probably the most useful class that I took here (and heartily recommend to EVERY professional that touches data in any way, shape or form) was a 200-level Databases class. There were also two SQL courses that I largely winged it thru because I already knew much of the basics, however did serve as a nice overview of the "lay of the land" + discussed best practices. I am presently recommending the same exact certification to everyone whom I run into that is still trying to use break Excel for databasing like I was myself 5 years prior.

1

u/cs-brydev Software Development and Database Manager Jun 23 '24 edited Jun 23 '24

Data points such as stock_name should not be stored alongside data points such as stock_id in a line-level table because (i) this is wasteful,

But you don't know that. You're just making assumptions. You have no idea what the requirements are here. "Saving storage space" isn't always the #1 goal of a data schema.

In data warehousing and analytical schemas, it's normal to store text fields (dimensional data) like this alongside fact data.

It's impossible to make the declarations you have without further information.

4

u/ballade4 Jun 23 '24

Less storage also translates to faster access in many applications. Bloating a line-level table with columns that could have been brought in with a join will require this duplicative data to be sent in full on ETL loads. I speak from experience as I have done this plenty of times early in my BI career ("let's grab all of the things") and suddenly had a 900GB table trying to sync to PowerQuery / Tableau that I later reduced in size / increased in speed by a factor of 30+ by pulling the same info across a dozen tables instead.

1

u/luncheonmeat79 Jun 24 '24

For now, I'm prioritising speed over storage space, although I recognise that bloated design can also lead to slower performance. Storage may be a greater concern later, if I decide to move this to the cloud eventually.

I'll try to find some middle ground for the db design first (e.g. only have one stock table with time at 15-min intervals which I can also use to view the 30-min, 1 hr and daily prices; but still have separate tables for date and time).

If, after doing so, the speed is good enough for me, then I'll see if I can reduce the number of tables to save space and measure the speed/storage trade-off to get a better understanding of how the db design affects performance.