On MySQL 5.7.34
I have this simple db structure. Important columns are domain name
, domain total links in
, but entries are duplicate (once for each found link from various sources), I need to select domain only once (link_to_domain
) and sort it by link_to_domain_total_links_in
DESC; Not sure how to render it here, here is the query:
CREATE TABLE IF NOT EXISTS `domain_to_domain_links` (
`id` int(11) NOT NULL,
`link_to_domain_hash` varchar(16) NOT NULL,
`link_to_domain_total_links_in` int(11) NOT NULL DEFAULT '0',
`link_to_domain` varchar(128) NOT NULL,
`link_from_domain` varchar(128) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=270245 DEFAULT CHARSET=utf8;
INSERT INTO `domain_to_domain_links` (`id`, `link_to_domain_hash`, `link_to_domain_total_links_in`, `link_to_domain`, `link_from_domain`) VALUES
(1, 'c9b13094745bae79', 3, 'example.com', 'from-other-site1.com'),
(1, 'c9b13094745bae79', 3, 'example.com', 'from-other-site2.com'),
(1, 'c9b13094745bae79', 3, 'example.com', 'from-other-site3.com'),
(2, 'c43f16c897f72994', 2, 'foo.com', 'from-other-site4.com'),
(3, 'c43f16c897f72994', 2, 'foo.com', 'from-other-site5.com');
This returns exactly what I need but from what I understand it actually counts the entries (as links) at query time, I need it to get it from the link_to_domain_total_links_in
so that it runs faster:
SELECT link_to_domain, COUNT(*) AS my_links_counter
FROM domain_to_domain_links
GROUP BY link_to_domain
ORDER BY COUNT(*) DESC;
Also link_to_domain_hash
is indexed, if it can take advantage of that when selecting it should be faster than link_to_domain
, but this is not critical.