I have two tables kw with 250000 records and t_n with 100000 records.
I joined both tables to get collective results in following query:
SELECT kw.id AS kw_id,
kw.word AS kw_word,
t_n.translation AS t_n_translation
FROM kw, t_n
WHERE kw.id = t_n.keyword_id
AND kw.word LIKE '1%'
GROUP BY t_n.translation
LIMIT 10
I also tried following simple join as an alternate of above query:
SELECT kw.id AS kw_id,
kw.word AS kw_word,
t_n.translation AS t_n_translation
FROM kw
INNER JOIN t_n
ON kw.id = t_n.keyword_id
AND kw.word LIKE '1%'
GROUP BY t_n.translation
LIMIT 10
But both queries took about 60 to 150 seconds depending on the system resources of memory and processing.
This execution timeout is unbearable for extending database and increasing multiple users.
Is there any efficient way to join two huge tables?
UPDATE
This query is for JQuery UI Autocomplete. And I used GROUP BY Clause to get distinct values values. Is there any other suitable solution to get distinct values.
Tables Structure
--
-- Table structure for table `kw`
--
CREATE TABLE `kw` (
`id` int(32) NOT NULL,
`word` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`pos` varchar(12) NOT NULL,
`definition` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `t_n`
--
CREATE TABLE `t_n` (
`id` int(16) NOT NULL,
`keyword_id` int(16) NOT NULL,
`translation` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`created_on` datetime NOT NULL,
`user_id` varchar(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `kw`
--
ALTER TABLE `kw`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `t_n`
--
ALTER TABLE `t_n`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `kw`
--
ALTER TABLE `kw`
MODIFY `id` int(32) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `t_n`
--
ALTER TABLE `t_n`
MODIFY `id` int(16) NOT NULL AUTO_INCREMENT;
COMMIT;