Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

panthro's avatar

Creating a feed - create dedicated table or use a view?

I need to build a feed for an initial set of 3 underlying tables, yet more may come later.

As far as I can see I have two options:

Create a MySQL view of a union of the 3 tables (or do a union at run time), access this via it's own model. Or...

When I save a row to an underlying table, also save a row to a feed table that has a polymorphic relation to one of the 3 underlying tables.

Which would be the best performance wise?

0 likes
6 replies
sr57's avatar

Performance depends on many parameters, for 'standard' use it should be no major difference but , I should use solution 1 (create a sql view) , it should be easier to implement, to maintain, ... just because mysql/postgresql takes care of at the db level.

1 like
martinbean's avatar

@panthro If you have a feed, then it’s probably best to have some form of FeedItem model with a polymorphic relationship pointing to the models as you mention. This way, you can just query the last n feed items instead of trying to construct UNION queries—this is essentially what a polymorphic relation attempts to extrapolate.

Use a polymorphic relation means you can also support additional feed item types in the future without having to change the underlying SQL statements. If you were just doing UNION statements, then you’d need to update the queries to support any new feed item types, or update them if you added or changed the columns or a corresponding feed item type.

1 like
panthro's avatar

Thanks for the advice, if I needed to update a view it would only be in one place.

If I was to use polymorphic on their own dedicated table, something like feed table, then every time I inserted a row into an underlying table, I would also have to update the parent relationship on the feed table.

Is this what you would suggest doing.

@sr57 suggests a view, which would manage that aspect for me.

I'm really stuck here with which direction to go and appreciate your answers so far.

martinbean's avatar

@panthro Yeah, you’d need to create a row in your “feed items” table as well as a row for the corresponding feed item model type. But if you use a polymorphic relation, it means you can make use of features like eager-loading relationships.

1 like
panthro's avatar

But if you made a model for a view then you could just eager load those relationships - no?

panthro's avatar

It's got to the stage when I need to implement this question, still undecided on a direction and was wondering if anyone else had a view?

At the minute I'm more leaning towards a feed table that polys the underlying models.

I would appreciate any opinions as I don't really want to screw this up!

Please or to participate in this conversation.