简体   繁体   中英

SQLite - Query data every time or store values in table?

I have currently two tables: TableA and TableB. Both tables are having about ~10-15 columns, and TableB got a foreign key on TableA's primary key. In TableB I am storing if one row is marked/checked. TableA is usually in relation with about 100-500 rows of TableB(foreign key). It looks like that:

在此处输入图片说明

In my android application I frequently have to retrieve all data of TableA and additionally get this values (all limited to the specific row id of TableA):

  • count of rows of TableB which were marked(set to 1)
  • count of all rows of TableB
  • the row of TableB with the highest Id, which is not marked(set to 0)

The standard solution and my first idea was that I query to get the marked rows and query to get the row with the highest Id for every row of TableA. But would that be with good performance? It should be getting retrieved as fast as possible, since all rows of TableA will get frequently retrieved.

So my other solution was to store the appropriate data in the columns of TableA, so I don't have to query the rows of TableB. That would look like that.:

在此处输入图片说明

But my question is: Would the second solution make that much of a difference performance-wise? Do we notice, that it would get faster retrieved? Does SQLite can query that much data that fast? Or would the extra effort not be worth to manage additional fields of TableA?

Maybe that kind of a question is already answered, but I didn't know what to look for.

在此处输入图片说明 I did a similar analysis comparing one table which stores all data in a long row (190 bytes to 400 bytes, average 221 bytes), and a two tables relational hierarchy with the child table row length 120 bytes long. Test results: Two tables hierarchy query execution time is 1/2 of the long table's in a LAN environment. The 2 tables query ran faster, although requires a join operation. But, oddly, more data are transferred from server to client than the one table approach. Since my test is done with wired connections, network speed was not considered, which may be significant for wireless connections. 2-![一个表查询速度较慢 table ] [1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM