Answer the question
In order to leave comments, you need to log in
How to assign Primary Key in MySQL view?
There is a view.
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `all_materials_test`
AS SELECT
`news`.`id` AS `id`,
`news`.`title_uk` AS `title_uk`,
`news`.`title_ru` AS `title_ru`,
`news`.`image_url` AS `image_url`,
`news`.`created_at` AS `created_at`,
`news`.`category_id` AS `category_id`,1 AS `is_news`,NULL AS `author_id`,
`news`.`label_text_uk` AS `label_text_uk`,
`news`.`label_text_ru` AS `label_text_ru`,
`news`.`importance` AS `importance`,
`news`.`label_color` AS `label_color`,
`news`.`announcement_uk` AS `announcement_uk`,
`news`.`announcement_ru` AS `announcement_ru`,
`news`.`content_uk` AS `content_uk`,
`news`.`content_ru` AS `content_ru`,
`news`.`alias_uk` AS `alias_uk`,
`news`.`alias_ru` AS `alias_ru`
FROM `news` union all select `material`.`id` AS `id`,
`material`.`title_uk` AS `title_uk`,
`material`.`title_ru` AS `title_ru`,
`material`.`image_url` AS `image_url`,
`material`.`created_at` AS `created_at`,
`material`.`category_id` AS `category_id`,0 AS `is_news`,
`material`.`author_id` AS `author_id`,
NULL AS `label_text_uk`,
NULL AS `label_text_ru`,
NULL AS `importance`,
NULL AS `label_color`,
`material`.`announcement_uk` AS `announcement_uk`,
`material`.`announcement_ru` AS `announcement_ru`,
`material`.`content_uk` AS `content_uk`,
`material`.`content_ru` AS `content_ru`,
`material`.`alias_uk` AS `alias_uk`,
`material`.`alias_ru` AS `alias_ru` from `material`;
Answer the question
In order to leave comments, you need to log in
primary key is a table property that makes each record unique. Hence the moral: is it possible to get uniqueness in a union expression? The projection always uses natural keys when sampling. That is, they will automatically be used. If there are brakes, then you need to figure out why and build the necessary indexes.
Here is a good article about this case
code.openark.org/blog/mysql/7-ways-to-convince-mys...
Upd:Here is another article about index management
dev.mysql.com/doc/refman/5.7/en /index-hints.html
Although it may be in the performance of the server or the settings of the database itself.
Indexes cannot be created directly in a view, not like in mysql, but in MSSQL you can.
As I understand it, mysql transfers indexes from a table, but this does not work with a calculated field.
Try to rewrite the query without union
Alternatively, create a cron table instead of view and fill
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question