views:

234

answers:

3

I need to display a single list, ordered by date which contains different types of data, images, video and whatnot.

I guess you could have one separate table for each type and the use something like FULL OUTER JOIN (simulated as sqlite doesn't support it) and sort on date. But this would be complicated because I still need to have unique IDs across all tables.

Maybe having a master list which has ID and date, and then JOIN in the types tables? But how bad is the performance of this using sqlite? I really like to avoid having a super table which contains columns all the combined types could ever need.

And ideas?

A: 

Is this something you want to do in database? I am guessing in code you map the data into different kinds of objects (e.g. class Video, class Image...) so you could the data combining there as well.

ponzao
Yes this is something I want to do in the database. I don't understand the other part of your answer :c
alexanderblom
Sorry for the being unclear. My idea was that if you have different types of data in the database, lets say you have a Video(id, date...) and Image(id, date...) then you might as well have the same types as Java classes. This enables you to do the sorting in code. I am guessing you are currently not doing any object-relation mapping?
ponzao
+1  A: 

You should try to build and see what performance hit you get.
SQLite is really fast engine, on mobile devices the flash memory is slower. But even with ten-thousands of records you get fast queries.

You shouldn't monkey with the performance hit until you don't have it.

Pentium10
Sure, but I'm mostly wondering if this is a good database scheme. If I should go with a master list or not?
alexanderblom
I would make a table named `listentry` having id, date, etc... And then using additional tables to store additional data for specific types such as `listentry_comments`, `listentry_images`, `listentry_video`.
Pentium10
And then multiple LEFT JOINs for the additional tables?
alexanderblom
Either join, or just do another query when you need it to display those. Be-aware that on the list you don't display all of the values just the name of the item, there is no purpose to join the tables for that.
Pentium10
Ah, yeah. I'll need to join as will want to display (at least some) things inline, like images. Thanks!
alexanderblom
+1  A: 

There are three patterns I know of for mapping class hierarchies to tables of a relational database:

  1. One table for the entire hierarchy. Every row will contain the union of all the attributes of the classes of the hierarchy. This is in my opinion hard to maintain if there are changes to the classes. Every row needs a type identifier, of course.
  2. One table for every concrete class of the hierarchy. In that case, a table that represents a concrete class contains the attributes of this class plus the ones of all abstract base classes. Abstract classes are not mapped directly.
  3. One table for every abstract and concrete class. In that case, every single class is directly mapped to a single table.

The advantage of 1. is that it is fairly easy to achieve what you want to do, i.e. read all objects that are part of your class hierarchy. You waste some space, though. In 2., you could read every table in a sub select of one select statements with constant dummy values for the attributes of the other classes, then UNION them together in a single SQL statement. In 3. the query gets a little more complicated: you have to INNER JOIN the concrete class tables with the base-class tables in the subselects. In neither case would you need a FULL OUTER JOIN, though.


Addendum:

Suppose you have an object hierarchy with a Person class and two subclasses, Student and Teacher. In 2., you'd create two tables, STUDENT and TEACHER, with both have a column NAME. The STUDENT table has an additional column YEAR, while the TEACHER table has a column SALARY. Your query might look like this:

    SELECT 'STUDENT' AS TYPE, NAME, YEAR, NULL AS SALARY FROM STUDENT 
UNION SELECT 'TEACHER' AS TYPE, NAME, NULL AS YEAR, SALARY FROM TEACHER

which you can then retrieve and use to fill your objects with data or display using a SimpleCursorAdapter or whatever you like.

Robert Petermeier
Could you illustrate #2? Or just tell me how to make the query?
alexanderblom
But that requires you to use have the same columns (null as year). Is there no way to do it without having to do that? Like using joins on a master table. I tried that myself but can't figure out on how to join against the master table for every join as joins use the last statement.Something like this only that all joins are against the entry table: http://pastebin.com/W6paVxQ9
alexanderblom