mysql - Optimal Way to Setup Table Structure -
i designing new mysql backend system , faced issue , unsure of optimal way setup table. let's have table called cities. each city has relation table weather contains weather information every month. simple let's structure of cities table is:
--------------- | id | name | --------------- | 1 | city1 | | 2 | city2 | | 3 | city2 | ---------------
for weather, have approximately 50 fields. each month of year have high temp, low temp, avg temp, avg precipitation. having table 50 columns seems inefficient , seems difficult add new fields. other option can consider have table following setup:
---------------------------------------------------- | id | cityid | type | value | unit | month | ---------------------------------------------------- | 1 | 1 | high_temp | 50 | f | 1 | | 2 | 1 | low_temp | 35 | f | 1 | | 3 | 1 | avg_temp | 45 | f | 1 | | 4 | 1 | avg_prec | 10 | in | 1 | | 5 | 2 | high_temp | 60 | f | 1 | | 6 | 2 | low_temp | 50 | f | 1 | | 7 | 2 | avg_temp | 55 | f | 1 | | 8 | 2 | avg_prec | 2 | in | 1 | ----------------------------------------------------
however, have around 1000 cities in database , extending tens of thousands. having table around 5 million rows seems excessive , worry query time.
what optimal way set up, one-to-one relationship or one-to-many relationship? also, more efficient creating complex queries in try search cities based on multiple weather characteristics?
currently have shown not normalized structure. imagine questions like:
what cities high temp greater 30 , low temp less 15 in january?
for city 1 when max temp , min temp exceeded?
query complicated ones. , questions involving several types of temp give headache in future.
the optimal way move temp values columns:
id | cityid | hightemp | lowtemp | avgtemp | avgprec | unit | month |
now abovemensioned query simple select statement appropriate clause. need ask similar questions , think how whould answer in concrete schema.
and believe, 1 millions of rows nothing database engine appropriate indexing.
Comments
Post a Comment