MySQL Forums
Forum List  »  Newbie

Slow queries. need adivce on creating indexes
Posted by: Orbiz Gam
Date: July 26, 2015 06:08AM

Hi,

I have a gaming portal that is powered by PHP+MySql.
the queries take too long (some take about half a second).
I think it's a problem with the indexes of the tables.

bellow is the datbase starcture including it's indexes and number of rows, followed by the quries I run (there are just a couple)

The database structure:
-- phpMyAdmin SQL Dump
-- version 3.4.10.1
-- Server version: 5.5.41
-- PHP Version: 5.2.2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Table structure for table `categories`
--

CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`active` varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`order` char(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`parent` varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
`home` varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
`desc` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`pid` varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=25 ;

/*
Indexes:
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 21 A

Row Statistics:
Format dynamic
Collation utf8_unicode_ci
Rows 21
*/

-- --------------------------------------------------------

--
-- Table structure for table `featuredgames`
--

CREATE TABLE IF NOT EXISTS `featuredgames` (
`gameid` int(11) unsigned NOT NULL,
`add_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`gameid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

/*
Indexes:
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No gameid 23 A

Space usage Type Usage
Data 207 B
Index 2,048 B
Total 2,255 B

Row Statistics:
Format static
Collation utf8_general_ci
Rows 23
*/
-- --------------------------------------------------------

--
-- Table structure for table `games`
-- (this is the big one with over 20,000 records)

CREATE TABLE IF NOT EXISTS `games` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` text COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`instructions` text COLLATE utf8_unicode_ci NOT NULL,
`keywords` text COLLATE utf8_unicode_ci NOT NULL,
`file` text COLLATE utf8_unicode_ci NOT NULL,
`height` int(11) NOT NULL DEFAULT '0',
`width` int(11) NOT NULL DEFAULT '0',
`category` int(11) NOT NULL DEFAULT '0',
`plays` int(11) NOT NULL DEFAULT '0',
`code` text COLLATE utf8_unicode_ci NOT NULL,
`type` enum('IMAGE','MOV','MPG','AVI','FLV','WMV','SWF','DCR','UNITY','YOUTUBE','CustomCode') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'SWF',
`source` enum('OTHER','FGD','FOG','KONGREGATE','AGF','MGF') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'OTHER',
`sourceid` int(11) unsigned NOT NULL,
`thumbnail` text COLLATE utf8_unicode_ci NOT NULL,
`ismochi` int(10) NOT NULL DEFAULT '0',
`thumbnail_200` varchar(255) CHARACTER SET utf8 NOT NULL,
`screen1` varchar(255) CHARACTER SET utf8 NOT NULL,
`screen2` varchar(255) CHARACTER SET utf8 NOT NULL,
`screen3` varchar(255) CHARACTER SET utf8 NOT NULL,
`screen4` varchar(255) CHARACTER SET utf8 NOT NULL,
`review` text CHARACTER SET utf8 NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `source` (`source`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=29576 ;

/*
Indexes:
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 25965 A
source BTREE No No source 4 A

Space usage Type Usage
Data 8,618.9 KiB
Index 567.0 KiB
Total 9,185.9 KiB

Row Statistics:
Format dynamic
Collation utf8_unicode_ci
Rows 25,965
*/
-- --------------------------------------------------------

--
-- Table structure for table `ratingsbar`
--

CREATE TABLE IF NOT EXISTS `ratingsbar` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`total_votes` int(11) NOT NULL DEFAULT '0',
`total_value` int(11) NOT NULL DEFAULT '0',
`used_ips` longtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=29576 ;

/*
Indexes:
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 1797 A

Space usage Type Usage
Data 42,440 B
Index 23,552 B
Total 65,992 B

Row Statistics:
Format dynamic
Collation latin1_swedish_ci
Rows 1,797
*/
-- --------------------------------------------------------


/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


---------------------------------------------------------
END database stracture.

Now these are the main queries I'm using (to my understanding, this information is important when it comes to indexes):
I don't use any UPDATE or INSERT queries for these tables, I just use SELECT queries:
------------
query to get featured games:
"SELECT `id` , `title` , `thumbnail` , `description` , `plays`
FROM `games`
INNER JOIN `featuredgames` ON `games`.`id` = `featuredgames`.`gameid`
where active='1'
ORDER BY rand()
LIMIT 50 "

query to get random games (slow query... takes half a second to run):
"SELECT games.id, games.title, games.thumbnail, games.description, games.plays, categories.name
FROM games
INNER JOIN categories ON games.category=categories.id
WHERE games.active='1'
AND categories.id NOT IN (8,19,12,24)
ORDER BY rand()
LIMIT 20"

get top rated games ordered by ratings:
"SELECT `games`.`id`, `title`, `thumbnail`, `description`, `plays`
FROM `games` INNER JOIN `ratingsbar` ON `games`.`id` = `ratingsbar`.`id`
where active='1' AND `ratingsbar`.`total_votes` > 0
ORDER BY `ratingsbar`.`total_value`/`ratingsbar`.`total_votes` DESC, `ratingsbar`.`total_votes` DESC LIMIT 90"

get most played games :
"SELECT `games`.`id`, `title`, `thumbnail`, `description`, `plays`
FROM `games` where active='1' order by plays DESC LIMIT 10"

get games in certain category ordered by ratings (this one takes too long, about half a second):
"SELECT `games`.`id`, `games`.`title`, `games`.`thumbnail`, `games`.`description`, `games`.`plays` , `ratingsbar`.`total_value`/`ratingsbar`.`total_votes` as ratings
FROM `games` LEFT JOIN `ratingsbar` ON `games`.`id` = `ratingsbar`.`id`
where active='1' AND category = " . $id . "
ORDER BY `ratingsbar`.`total_value`/`ratingsbar`.`total_votes` DESC, `ratingsbar`.`total_votes` DESC LIMIT 90"

get deatils of certain game: "SELECT * FROM `games` where active='1' and id = ".$id
----------

that's it.
are my indexes ok ? should I add more ?

Options: ReplyQuote


Subject
Written By
Posted
Slow queries. need adivce on creating indexes
July 26, 2015 06:08AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.