staudenmeir / eloquent-json-relations

Laravel Eloquent relationships with JSON keys
MIT License
1k stars 63 forks source link

How to multi-valued indexes on mariadb #112

Closed timelabs-lucvt closed 4 months ago

timelabs-lucvt commented 4 months ago

I have a promotions table and a product_ids column containing a list of products. When I do a migration, I get the same error.

Schema::table('promotions', function (Blueprint $table) { $table->rawIndex('(cast(product_ids as unsigned array))', 'product_ids_index'); });

Is there any other way to fix it? Thank you!

staudenmeir commented 4 months ago

Hi @timelabs-lucvt, MariaDB doesn't support multi-valued indexes, unfortunately.

timelabs-lucvt commented 4 months ago

@staudenmeir So is there any way to solve it other than changing the data structure?

staudenmeir commented 4 months ago

There's no alternative that I know of.

Are you having performance issues?

timelabs-lucvt commented 4 months ago

That's right. I am querying for products that are not yet part of any promotions. And it's quite slow

staudenmeir commented 4 months ago

What query are you running in this case?

timelabs-lucvt commented 4 months ago

In the product model I define the hasManyJson promotions relationship

image

and I use query

image

staudenmeir commented 4 months ago

Did you already log the executed SQL and analyze it with EXPLAIN?

Maybe you can improve the query performance with indexes on the time or status columns (if you haven't already). If the lookup in the product_ids column is the actual bottleneck, I don't really see any solutions besides changing the data structure.

timelabs-lucvt commented 4 months ago

I have indexed the time and status but the query is still quite slow. I decided to change the data structure and it got better. Thanks for your support