如何在mysql视图表中添加索引

时间:2018-09-13 05:17:39

标签: php mysql wordpress

CREATE VIEW test AS SELECT
wp_posts.ID,
wp_posts.post_title,
STR_TO_DATE(pm1.meta_value,'%c/%e/%Y') AS expiration_date,
pm2.meta_value AS ceadmin_email,
pm3.meta_value AS student_email,
pm4.meta_value AS email,
pm5.meta_value AS cefirst_name,
pm6.meta_value AS celast_name
FROM wp_posts
LEFT JOIN wp_postmeta AS pm1 ON (wp_posts.ID=pm1.post_id AND pm1.meta_key='ceexpiration_date')
LEFT JOIN wp_postmeta AS pm2 ON (wp_posts.ID=pm2.post_id AND pm2.meta_key='ceadmin_email')
LEFT JOIN wp_postmeta AS pm3 ON (wp_posts.ID=pm3.post_id AND pm3.meta_key='secondary_email')
LEFT JOIN wp_postmeta AS pm4 ON (wp_posts.ID=pm4.post_id AND pm4.meta_key='email')
LEFT JOIN wp_postmeta AS pm5 ON (wp_posts.ID=pm5.post_id AND pm5.meta_key='cefirst_name')
LEFT JOIN wp_postmeta AS pm6 ON (wp_posts.ID=pm6.post_id AND pm6.meta_key='celast_name')
WHERE wp_posts.constraint_type='PRIMARY KEY'
AND wp_posts.table_schema=DATABASE()
AND wp_posts.table_name='owalog'; wp_posts.post_type='student_certificates' AND pm2.meta_value IN (SELECT user_email FROM supervisorcount)

1 个答案:

答案 0 :(得分:0)

在MySQL documentation上查看:

  

无法在视图上创建索引。

因此,您确实有两个可用的选项:

  • 您可以通过在涉及的实际表上添加适当的索引来调整视图基础的查询,或者
  • 您可以使用视图后面的查询创建临时表,然后直接对其建立索引。

MySQL没有任何正式的物化视图,但是一个临时表与此非常接近。请注意,通常,(未实现的)视图将只执行与基础查询相同的操作。