我们有2个表格,其中提供了学校列表。
第一个表(school_data
)是来自公共数据库的原始数据,当政府发布新数据时会对其进行更新。
第二个表school_data_overwrites
具有大多数相同的字段,用于更正第一个表中的错别字和不准确性(位于另一个表中,以便能够在保留我们的编辑的同时更新数据)。< / p>
然后,我们使用代码应查询的视图,该视图的每一行应显示school_data_overwrites
中的值(如果存在),或回退到school_data
(如果不存在)。具有挑战性的是,在某些行的顶部有一个parent_school_id
链接到另一行,该行本身可能已经覆盖了数据。
我们到达以下视图,但是它非常慢并且占用大量CPU:
SELECT
school_data.id AS id,
school_data.ref_id AS ref_id,
school_data.schoolyear AS schoolyear,
IF(school_data_overwrites.type <> '' OR school_data_overwrites.type <> NULL, school_data_overwrites.type, school_data.type) AS type,
IF(school_data_overwrites.name <> '' OR school_data_overwrites.name <> NULL, school_data_overwrites.name, school_data.name) AS name,
IF(school_data_overwrites.email <> '' OR school_data_overwrites.email <> NULL, school_data_overwrites.email, school_data.email) AS email,
IF(school_data_overwrites.certified_email <> '' OR school_data_overwrites.certified_email <> NULL, school_data_overwrites.certified_email, school_data.certified_email) AS certified_email,
IF(school_data_overwrites.website <> '' OR school_data_overwrites.website <> NULL, school_data_overwrites.website, school_data.website) AS website,
IF(school_data_overwrites.address <> '' OR school_data_overwrites.address <> NULL, school_data_overwrites.address, school_data.address) AS address,
IF(school_data_overwrites.postcode <> '' OR school_data_overwrites.postcode <> NULL, school_data_overwrites.postcode, school_data.postcode) AS postcode,
school_data.city_id AS city_id,
school_data.city_name AS city_name,
school_data.province_id AS province_id,
school_data.province_name AS province_name,
school_data.province_iso_code AS province_iso_code,
school_data.region_id AS region_id,
school_data.region_name AS region_name,
school_data.nuts3_2010_code AS nuts3_2010_code,
school_data_overwrites.facebook_url AS facebook_url,
school_data_overwrites.twitter_url AS twitter_url,
school_data_overwrites.vip AS vip,
school_data.parent_school_id AS parent_school_id,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.city_id, '') parent_school_ref_id,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.city_id, '') parent_school_schoolyear,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.type <> '' OR parent_school_data_overwrites.type <> NULL, parent_school_data_overwrites.type, parent_school_data.type),
''
) AS parent_school_type,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.name <> '' OR parent_school_data_overwrites.name <> NULL, parent_school_data_overwrites.name, parent_school_data.name),
''
) AS parent_school_name,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.email <> '' OR parent_school_data_overwrites.email <> NULL, parent_school_data_overwrites.email, parent_school_data.email),
''
) AS parent_school_email,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.certified_email <> '' OR parent_school_data_overwrites.certified_email <> NULL, parent_school_data_overwrites.certified_email, parent_school_data.certified_email),
''
) AS parent_school_certified_email,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.website <> '' OR parent_school_data_overwrites.website <> NULL, parent_school_data_overwrites.website, parent_school_data.website),
''
) AS parent_school_website,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.address <> '' OR parent_school_data_overwrites.address <> NULL, parent_school_data_overwrites.address, parent_school_data.address),
''
) AS parent_school_address,
IF(
school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL,
IF(parent_school_data_overwrites.postcode <> '' OR parent_school_data_overwrites.postcode <> NULL, parent_school_data_overwrites.postcode, parent_school_data.postcode),
''
) AS parent_school_postcode,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.city_id, '') AS parent_school_city_id,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.city_name, '') AS parent_school_city_name,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.province_id, '') AS parent_school_province_id,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.province_name, '') AS parent_school_province_name,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.province_iso_code, '') AS parent_school_province_iso_code,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.region_id, '') AS parent_school_region_id,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.region_name, '') AS parent_school_region_name,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data.nuts3_2010_code, '') AS parent_school_nuts3_2010_code,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data_overwrites.facebook_url, '') AS parent_school_facebook_url,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data_overwrites.twitter_url, '') AS parent_school_twitter_url,
IF(school_data.parent_school_id <> '' OR school_data.parent_school_id <> NULL, parent_school_data_overwrites.vip, '') AS parent_school_vip
FROM
school_data LEFT JOIN school_data_overwrites ON school_data_overwrites.school_id = school_data.id,
school_data parent_school_data LEFT JOIN school_data_overwrites parent_school_data_overwrites ON parent_school_data_overwrites.school_id = parent_school_data.parent_school_id
WHERE parent_school_data.id = school_data.id;
school_data
看起来像这样:
+---------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------+--------------+------+-----+---------+-------+
| id | varchar(255) | YES | | NULL | |
| ref_id | varchar(255) | YES | | NULL | |
| schoolyear | int(11) | YES | MUL | NULL | |
| type | varchar(255) | YES | MUL | NULL | |
| name | varchar(255) | YES | MUL | NULL | |
| email | varchar(255) | YES | MUL | NULL | |
| certified_email | varchar(255) | YES | MUL | NULL | |
| website | varchar(255) | YES | | NULL | |
| address | varchar(255) | YES | MUL | NULL | |
| cad_code | varchar(255) | YES | MUL | NULL | |
| postcode | varchar(255) | YES | MUL | NULL | |
| city_name | varchar(255) | YES | MUL | NULL | |
| city_id | varchar(255) | YES | MUL | NULL | |
| province_name | varchar(255) | YES | MUL | NULL | |
| province_id | varchar(255) | YES | MUL | NULL | |
| province_iso_code | varchar(255) | YES | MUL | NULL | |
| region_name | varchar(255) | YES | MUL | NULL | |
| region_id | varchar(255) | YES | MUL | NULL | |
| nuts3_2010_code | varchar(255) | YES | MUL | NULL | |
| parent_school_id | varchar(255) | YES | MUL | NULL | |
这是school_data_overwrites
:
+---------------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| school_id | varchar(255) | NO | UNI | NULL | |
| type | varchar(255) | YES | MUL | NULL | |
| name | varchar(255) | YES | MUL | NULL | |
| email | varchar(255) | YES | MUL | NULL | |
| certified_email | varchar(255) | YES | MUL | NULL | |
| website | varchar(255) | YES | MUL | NULL | |
| address | varchar(255) | YES | MUL | NULL | |
| postcode | varchar(255) | YES | MUL | NULL | |
| city_id | varchar(255) | YES | | NULL | |
| facebook_url | varchar(255) | YES | | NULL | |
| twitter_url | varchar(255) | YES | | NULL | |
| email_they_read | tinyint(4) | YES | | 0 | |
| certified_email_they_read | tinyint(4) | YES | | 0 | |
| vip | tinyint(4) | YES | | 0 | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+---------------------------+------------------+------+-----+---------+----------------+
我们不知道如何优化它。我认为我们包含了太多条件,并且有一种更简单的方法。
我们如何使这种观点更具表现力?