Answer the question
In order to leave comments, you need to log in
How can I rewrite the structure of tables or queries so that the index works for my selections?
Brief description of what we have.
We collect some statistics of visits to websites. The table of visits, in a simplified form, looks like this:
Table: links
id | url | domain
1 | https://www.youtube.com/watch?v=6Nu3ZVA8Gic | com.youtube.www
2 | https://www.youtube.com/watch?v=5ww70Xb5pm8 | com.youtube.www
3 | http://www.bbc.com/ukrainian/politics | com.bbc.www
4 | http://bbc.com/ukrainian/business | com.bbc
Table: sites
id | name | domain | description
1 | YouTube | com.youtube | ...
2 | VKontake | com.vk | ...
3 | BBC | com.bbc | ...
SELECT id, url FROM links
WHERE domain = 'com.bbc' OR domain LIKE 'com.bbc.%'
SELECT links.id, links.url, sites.id AS site_id, sites.description
FROM links
LEFT JOIN sites ON links.domain = sites.domain
OR links.domain LIKE CONCAT(sites.domain, '.%')
Answer the question
In order to leave comments, you need to log in
To begin with, I will explain why the indexes in your case do not work and cannot work.
> OR links.domain LIKE CONCAT(sites.domain, '.%')
CONCAT is a function and you work with the result of the function.
Those. it turns out that in your request you need to:
1. select all lines from links
2. add to each line by sites.domain or sites.domain the result of the function.
=> need to count each line every time.
This is a lot.
what i would do:
1. created a table of domains
in it:
id | main_id |domain
1 | 1 | com.youtube
2. | 1 |com.youtube.www
3. | 1 |com.youtube.subdomain
SELECT links.id, links.url, sites.id AS site_id, sites.description
FROM links
LEFT JOIN domains ON links.domainId = domains.id
LEFT JOIN sites ON sites.id = domains.main_id
3NF will look like this
domain_zone:
parent = ForeignKey(domain_zone)
name = Text
site_page
domain_zone = ForeignKey(domain_zone)
url = URL
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question