SELECT party_code , max(date) AS date FROM
server1.table1 WITH (nolock) GROUP BY party_code
UNION
SELECT party_code , max(date) AS date FROM
server2.table1 WITH (nolock) GROUP BY party_code
UNION
SELECT party_code , max(date) AS date FROM
server3.table1 WITH (nolock) GROUP BY party_code
Like shown above I have similarly 17 tables on different servers, so I union them to get records. The total data sums up to more than 36 crores (360 millions) which effects the database execution time and ability to retrieve records. Can someone help me as to how to optimize this. Or any other solution to it.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table1` ADD INDEX `table1_idx_party_code` (`party_code`);
SELECT
server1.table1.party_code,
max(date) AS date
FROM
server1.table1 WITH (NOLOCK)
GROUP BY
server1.table1.party_code
UNION
SELECT
server2.table1.party_code,
max(date) AS date
FROM
server2.table1 WITH (NOLOCK)
GROUP BY
server2.table1.party_code
UNION
SELECT
server3.table1.party_code,
max(date) AS date
FROM
server3.table1 WITH (NOLOCK)
GROUP BY
server3.table1.party_code