Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:23:24+00:00 2026-06-14T03:23:24+00:00

I have this query , take long time when I executed it SELECT DISTINCT

  • 0

I have this query , take long time when I executed it

SELECT DISTINCT ticket.`id`, 
                `sender`, 
                `text`, 
                `receivedtime`, 
                `priorityid`, 
                `cityid`, 
                `categoryid`, 
                `statusid`, 
                `activeuserid`, 
                `note`, 
                `operationid`, 
                '' AS SMSHistory, 
                '' AS replySMS, 
                '' AS ticketHistory 
FROM   `ticket` 
       LEFT OUTER JOIN tickethistory 
                    ON tickethistory.ticketid = ticket.id 
       LEFT OUTER JOIN users 
                    ON tickethistory.uid = users.uid 
ORDER  BY ticket.`id` DESC 
LIMIT  0, 50 

and here is the table structure :

Ticket:

CREATE TABLE   `ticket` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `sender` varchar(50) NOT NULL,
    `text` text NOT NULL,
    `receivedTime` datetime NOT NULL,
    `priorityId` int(10) NOT NULL,
    `cityId` int(10) NOT NULL,
    `categoryId` int(10) NOT NULL,
    `statusId` int(10) NOT NULL,
    `activeUserId` int(11) NOT NULL,
    `note` text NOT NULL,
    `operationId` int(10) NOT NULL,
    `gateway` varchar(80) NOT NULL,
    KEY `Index 1` (`id`),
    KEY `sender` (`sender`),
    KEY `priorityId` (`priorityId`),
    KEY `cityId` (`cityId`),
    KEY `categoryId` (`categoryId`),
    KEY `statusId` (`statusId`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  ----

Tickethistory :

  CREATE TABLE   `tickethistory` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `ticketId` int(10) NOT NULL,
    `uid` int(11) NOT NULL,
    `actionId` int(10) NOT NULL,
    `time` datetime NOT NULL,
    `param1` text NOT NULL,
    `param2` text NOT NULL,
    `param3` text NOT NULL,
    KEY `Index 1` (`id`),
    KEY `ticketId` (`ticketId`),
    KEY `uid` (`uid`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  ---- 

users :

CREATE TABLE IF NOT EXISTS `users` (
  `Uid` int(11) NOT NULL AUTO_INCREMENT,
  `UserName` varchar(255) NOT NULL,
  `Upassword` varchar(32) NOT NULL,
  `UName` text NOT NULL,
  `Ucountry` text NOT NULL,
  `Umobile` varchar(255) NOT NULL,
  `UregisterDate` date NOT NULL DEFAULT '0000-00-00',
  `Ugroup` char(1) NOT NULL DEFAULT 'U',
  `Usender` varchar(11) NOT NULL DEFAULT 'SMS',
  `Ucredits` decimal(11,2) NOT NULL DEFAULT '0.00',
  `Uemail` varchar(255) NOT NULL,
  `CreditUpdatedDate` date DEFAULT NULL,
  `USMSC` varchar(30) NOT NULL,
  `repeatedDuration` int(10) DEFAULT '0',
  `langid` varchar(10) DEFAULT 'Ar',
  `parentId` int(11) NOT NULL DEFAULT '0',
  `Usess` varchar(255) NOT NULL DEFAULT '0',
  PRIMARY KEY (`Uid`),
  UNIQUE KEY `UserName` (`UserName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Here is the explain command result:

  'id';'select_type';'table';'type';'possible_keys';'key';'key_len';'ref';'rows';'Extra'
  '1';'SIMPLE';'ticket';'ALL';'';'';'';'';'348580';'Using temporary; Using filesort'
  '1';'SIMPLE';'tickethistory';'ref';'ticketId';'ticketId';'4';'ticket.id';'2';'Distinct'
  '1';'SIMPLE';'users';'eq_ref';'PRIMARY';'PRIMARY';'4';'tickethistory.uid';'1';'Using index; Distinct'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:23:25+00:00Added an answer on June 14, 2026 at 3:23 am

    The EXPLAIN suggests that it’s using a temporary table and filesort to order the records in the TICKET table. That’s a little weird, because there is an index on ID which you’re sorting on, but given it matches 350K records, that’s probably why it’s slow.

    As you’re trying to find the latest records, try to include a limiting “where” clause, for instance by limiting the search to the last week (don’t forget to create an index on receivedTime).

    You might also consider not having an outer join from ticketHistory to User – the UID column is NOT NULL, so there should be no records without a matching user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query... SELECT Distinct([TargetAttributeID]) FROM (SELECT distinct att1.intAttributeID as [TargetAttributeID] FROM AST_tblAttributes
I have a MySql query that take a very long time to run (about
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
I have this query SELECT zip, ( 3959 * acos( cos( radians(34.12520) ) *
I have this query which is a dependant query and taking much execution time
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have some screens making some data queries that might take a long time.
I have a query which takes a long time and I want to optimize
I have this query which on executing in my sql command line client executes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.