The Web Hosting Guide
Infotx
Welcome to Infotx - Webmaster Guides and Resources.
Web hosting using MySQL.MySQL
|
I keep getting CPU Exceeded Errors, What are some things I can do to fix this? |
|
Ordering by surname is a common requirement, so it would make sense to create an index on surname. But in this example our employee table consists of thousands of people from Swaziland, and with the surname "Dlamini". So we need to index on firstname as well. The good news is that MySQL uses leftmost prefixing, which means that a multi-field index A,B,C will also be used to search not only for a,b,c combinations, but also A,B as well as just A. +--------+------+-------------------+---------+---------+-------+------+-----------+ |table | type | possible_keys | key | key_len | ref | rows |Extra | +--------+------+-------------------+---------+---------+-------+------+-----------+ |employee| ref | surname,surname_2 | surname | 41 | const | 1 |where used | +--------+------+-------------------+---------+---------+-------+------+-----------+ However, the query +----------+------+---------------+------+---------+------+------+------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +----------+------+---------------+------+---------+------+------+------------+ | employee | ALL | NULL | NULL | NULL | NULL | 3 | where used | +----------+------+---------------+------+---------+------+------+------------+ If you needed this kind of query, you would have to add a separate index on firstname. The Query Optimizer, OPTIMIZE and ANALYZEThe magic inside MySQL that decides which keys, if any, to use to in the query, is called the query optimizer. It takes a quick glance at the index to see which indexes are the best to use. Compare it to searching for a CD by the artist "Savuka", called "Third World Child", where there are 2 indexes, one alphabetical by artist name, and the other by album name. At a glance, you see that there are 20000 unique artists, and 400000 unique albums, so you decide to search by artist. But if you knew that there were 50 Savuka albums, and that Third World child is the only album starting with "T", your search criteria would change. You can provide similar information for the Optimizer by running |
|
What character sets are supported for mysql. |
|
Log in to cPanel. |
|
How do I configure Dreamweaver MX for PHP and MySQL? |
|
Remote Info: |
|
How do I import a database using command line? (SSH) |
|
**SSH access is needed for this process. |
|
I keep getting CPU Exceeded Errors, What are some things I can do to fix this? |
|
Some knowledge of how indexes work allows you to use them more efficiently. Firstly, note that when you update a table with an index, you have to update the index as well, so there is a performance price to pay. But unless your system runs many more inserts than selects and the inserts need to be quick, and not the selects, this is a price worth paying. +----------+------+---------------+------+---------+------+------+------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +----------+------+---------------+------+---------+------+------+------------+ | employee | ALL | NULL | NULL | NULL | NULL | 2 | where used | +----------+------+---------------+------+---------+------+------+------------+ Not good at all! Every single employee record is being read. Why is this? The answer lies in the "overtime_rate/2" part of the query. Every overtime_rate (and hence every record) has to be read in order to divide it by 2. So we should try and leave the indexed field alone, and not perform any calculations on it. How is this possible? This is where your school algebra comes to the rescue! You know that 'x/2 = y' is the same as 'x = y*2'.We can rewrite this query, by seeing if the overtime_rate is less than 20*2. Let's see what happens. +--------+-------+---------------+---------------+---------+------+------+----------+ |table | type | possible_keys | key | key_len | ref | rows |Extra | +--------+-------+---------------+---------------+---------+------+------+----------+ |employee| range | overtime_rate | overtime_rate | 4 | NULL | 1 |where used| +--------+-------+---------------+---------------+---------+------+------+----------+ Much better! MySQL can perform the 20*2 calculation once, and then search the index for this constant. The principle here is to keep your indexed field standing alone in the comparison, so that MySQL can use it to search, and not have to perform calculations on it. |
|
How do I create an empty MySQL Database? |
|
To create the Database, you would go to your cpanel |
|
What version of MySQL are available? |
|
Most hosting companies currently run the MySQL version 4.1.14-standard. They usually upgrade when cPanel upgrades their software. |
|
How do I create a MySQL backup? |
|
There are two ways to go about backing up your MySQL databases. |
|
I keep getting CPU Exceeded Errors, What are some things I can do to fix this? |
|
You know the scene. The database is just too slow. Queries are queuing up, backlogs growing, users being refused connection. Management is ready to spend millions on "upgrading" to some other system, when the problem is really that MySQL is simply not being used properly. Badly defined or non-existent indexes are one of the primary reasons for poor performance, and fixing these can often lead to phenomenal improvements. Consider an extreme example: CREATE TABLE employee ( employee_number char(10) NOT NULL, firstname varchar(40), surname varchar(40), address text, tel_no varchar(25), salary int(11), overtime_rate int(10) NOT NULL ); To find employee Fred Jone's salary(employee number 101832), you run: SELECT salary FROM employee WHERE employee_number = '101832'; EXPLAIN SELECT employee_number,firstname,surname FROM employee WHERE employee_number= '10875'; +----------+------+---------------+------+---------+------+------+------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +----------+------+---------------+------+---------+------+------+------------+ | employee | ALL | NULL | NULL | NULL | NULL | 2 | where used | +----------+------+---------------+------+---------+------+------+------------+ So what are all these things?
Looks like our query is a shocker, the worst of the worst! There are no possible keys to use, so MySQL has to go through all the records (only 2 in this example, but imagine a really large table). +----------+-------+---------------+---------+---------+-------+------+-------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +----------+-------+---------------+---------+---------+-------+------+-------+ | employee | const | PRIMARY | PRIMARY | 10 | const | 1 | | +----------+-------+---------------+---------+---------+-------+------+-------+ The query above is a good one (it almost falls into the category of "couldn't be better"). The type of "join" (not really a join in the case of this simple query) is "const", which means that the table has only one matching row. The primary key is being used to find this particular record, and the number of rows MySQL thinks it needs to examine to find this record is 1. All of which means MySQL could have run this query thousands of times in the time it took you to read this little explanation. |
|
I get permission denied errors when I try to run certain commands in mysql. |
|
There are a few mysql queries that most hosting companies don't allow customers to use with their main account user(or any user for that matter). One of these commands is CREATE database. The only user that can create databases on many hosting companies systems is the mysql root user, and the only way to use its priveleges to create a new database is through the cpanel, under 'Mysql Databases'. Most hosting companies also don't allow either commands for LOAD DATA INFILE/OUTFILE, for security reasons. There are some alternatives to this, if you're trying to import or export a database. Here's two examples: |
|
I have a backup of my database that I need to import/transfer to my new account. |
|
To do this you will need the file in .sql format, meaning it can not be compressed in a .zip or .tar.gz file. |
|
How do I make my existing MySQL database compatible with the version provided? |
|
If your version of MySQL is a later release than ours, you can export the database to make it compatible with ours. To do this you would run the below command from the command prompt on your server. |
|
What are the Basic Connection Settings for MySQL? |
|
There are two different ways to connect to a MySQL database. Locally (from a script on the server, to the server), and Remotely (from a remote computer, using Shell, ODBC, or Navicat) |
|
I would like to dump the Table Structure for my MySQL Database, but none of the data. |
|
The command line option from SSH is: |
|
I can't log into MySQL from a remote computer using a MySQL admin tool(phpMyAdmin, Navicat, MySQLFront, MySQL-Admin, etc). |
|
Our firewalls automatically block out any requests to connect to a MySQL database from a remote computer(anything other than localhost), unless your ip address is white listed on many hosting companies firewall. |
|
I can't connect to my database locally or remotely, do I have the correct username and password? |
|
Since most hosting companies run a shared hosting environment, most hosting companies have to uniquely identify your usernames to your account ONLY. They do this by attaching your system user name and an underscore("_") before all the databases and users YOU create. |
|
When logging into phpMyAdmin, why do I get a 404 not found error? |
|
This happens if you have logged into the cPanel. In order for phpMyAdmin to work, you must log in from yourdomainname.com/cpanel. |
|
How can I connect to our MySQL database from Dream Weaver? |
|
Follow the below steps very carefully. |
|
How do I restore a MySQL database? |
|
1. Login to your control panel http://yourdomain.com/cpanel |
|
How to import and export a MySQL Database |
|
MySQL Tutorial - Import or Export A Database |
|
How do I connect to my Database using MySQLfront? |
|
If you wish to administer your database from your own PC, via a program like MySQLFront, you will need to add an "access host" via the MySQL Database Icon in the cpanel. |
|
How do I create a MySQL database? |
|
1. Login to your control panel http://yourdomain.com/cpanel |
|
Problem with old host and new host, my database still is looking at the old host. |
|
When importing an old database from another host you must go through a multi-step process or it could possibly link back to the old host until it is directed to change by a fast mod available at the PHP Knowledgebase: |
|
I keep getting CPU Exceeded Errors, What are some things I can do to fix this? |
|
Most systems need to be highly optimized for selects - take a news site which performs millions of queries per day, but where the data arrives in large batches of text files. So for parts of the day, inserts need to be optimal, without noticeably affecting the millions trying to access the data... if (!($fp = fopen("datafile.txt","r"))) { // open the file for reading print "nUnable to open datafile.txt for writing"; // display error exit(); // end the running of the program } while (!feof ($fp)) { // loop through the file line by line $sline = fgets($fp, 4096); // put the data into the variable $sline $sline = chop($sline); // remove the newline list($eno,$fname,$sname,$telno,$salary) = split("|",$code); // split the line on "|", populating the ind. variables $db->query("insert into employee(employee_number,firstname,surname, tel_no, salary values($eno,'$fname','$sname','$tel_no', $salary)"); } // end while loop This would work, but would be very slow. The index buffer would be flushed after every insert. Until recently, MyISAM tables (The MySQL default) did not allow data to be inserted at the same time as being read. The new format does, but only if there are no deleted records present (highly unlikely in a heavily used system). So the entire table is locked for the duration of each insert. Fine on a low volume site, but when you're getting hundreds or thousands of queries per second, you'll soon notice the backlog! ConclusionIt's not only getting the data in that needs to be quick - sometimes you need to get it out quickly too. (Say you've accidentally loaded yesterday's classified ads, for example). Don't do a: |