Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have developed a web application called Referral Program, It it developed by using PHP, MySQL. Users can joining the website and when they introduce new members, he/she gets a commission. The system working smoothly when the number of users is low. But the number of users over 50,000, then the system gets slows.

The problem is users comes into the system is like binary trees, and when he/she enter into the system, a commission is distributes up to top level(Root node or admin). A tree traversing is need to be done on each registration. It takes some more time. Five (or more) types of commissions must be given to each user register in the system, when 100,000 users in the system 500,000 records will be there. Then the system gets too slow. And 600-1000 users will be online. They can't do anything because the slow.

At last we consolidate the 500,000 records to 100,000 (we keep the sum only), the n its OK. They can use the application. We changed InnoDB to MyISAM, its faster but doesn't support transaction. But we should keep the original records. How it to be done ?

EDITED - 1

I am listing the server features here.

  • Operating System : CentOS
  • RAM : 25 GB
  • Hard disc : 1500 GB
  • Current Data Base size : 800 MB
  • MySQL Version : 5.1 (or above)
  • PHP : 5.3

MySQL error “Deadlock found when trying to get lock; try restarting transaction”

We are running a cron job (daily basis), it needs more time to complete because complex calculation is running in the huge database. When a user register or login to the system, then shows the above error. I just look here and here dealing with the same error, but i didn't get the exact solution. We are doing lot of SELECT / UPDATE / INSERT

EDITED - 2 This is the database structure I used, also other table are there, but I think it is not important now.

Datastructure

The function I used is here

public function getAllUplineId($id,$i,$position,$profile_field_id){
$select = "SELECT father_id
            FROM 161_ft_individual
            WHERE id=$id ";
$result = db_query($select);    
while($node = db_fetch_object($result))
    {
    $user_id = $node->father_id;   
    if($user_id > 0)
        {
        $amount = 1;        
        $this->update_profile_value($user_id,$profile_field_id,$amount);
        $i=$i+1;         
        $this->getAllUplineId($user_id,$i,$position,$profile_field_id);
        }

    }
}

public function update_profile_value($user_id,$profile_field_id,$amount){
$query = "UPDATE 161_ft_individual SET total_leg=total_leg+$amount
            WHERE id='$user_id' LIMIT 1";
$result = db_query($query);    
return $result;
}

The getAllUplineId() function takes and update all upline users from the point who register. The number of users more than 50,000 (in table 161_ft_individual) it has become too slow and nobody can do anything. The table 161_leg_amount has 500,000 records.

Thanks in advance.

share|improve this question
3  
Can you add example DDL and the problem queries to your question? –  Mark Storey-Smith Oct 20 '11 at 18:43
1  
Specifically the EXPLAIN output of your problem queries. –  Derek Downey Oct 20 '11 at 19:09
    
@AshokKS is the SELECT query supposed to be from 161_ft_individual ? If not, can you post the structure and SHOW CREATE TABLE ft_individual output? –  Derek Downey Oct 21 '11 at 14:48
3  
Even if not for tuning reasons, you really should be using parameterized queries. If you're looping through thousands of users, as you suggest, you'll likely see some performance improvements, too. You may also want to focus on solving the deadlock issue directly, rather than just speeding up queries. As you specifically mentioned InnoDB, I'd have to ask -- what are your indexes? –  Joe Oct 21 '11 at 15:17
    
@DTest YES selecting and updating from same table 161_ft_individual, somehow the prefix deleted. –  Ashok KS Oct 22 '11 at 4:21

2 Answers 2

up vote 1 down vote accepted

The basic problem you have is that everything is recursive and you are attempting to lock pages of data in InnoDB, creating lots of MVCC info. In some case, you may have attempted to lock the same InnoDB page repeatedly. That explains the deadlocks. I have commented on this situation deeply with someone in three separate posts on this same issue:

You need a workaround. You may want to try the following algorithm:

For this example

  • id = 12
  • amount = 22.75

Step 01) Make a table to collect all father_ids to be updated

DROP TABLE IF EXISTS fathers_to_update;
CREATE TABLE fathers_to_update
(
    father_id INT NOT NULL,
    PRIMARY KEY (father_id)
);

Step 02) Making a table that will be a FIFO queue. Put the value of 12 into the queue:

DROP TABLE IF EXISTS fathers_to_queue;
CREATE TABLE fathers_to_queue
(
    father_id INT,
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);
INSERT INTO fathers_to_queue (father_id) VALUES (12);

Step 03) Get the Count of fathers_to_queue

SELECT COUNT(1) FROM fathers_to_queue;

If the count is 0, goto Step 07)

Step 04) Remove the front number in fathers_to_queue

SELECT MIN(id),father_id INTO @frontndx,@dad FROM fathers_to_queue;
DELETE FROM fathers_to_queue WHERE id = @frontndx;

Step 05) Collect all father_ids from 161_ft_individual whose id is @dad into the queue

INSERT INTO fathers_to_update SELECT father_id FROM 161_ft_individual WHERE id = @dad;
INSERT INTO fathers_to_queue (father_id) SELECT father_id FROM 161_ft_individual WHERE id = @dad;

Step 06) Go back to Step 03

Step 07) Perform a single UPDATE of all records in 161_ft_individual you collected in fathers_to_update

UPDATE fathers_to_update A
LEFT JOIN 161_ft_individual B
USING (father_id)
SET B.total_leg=B.total_leg+@amount;

That's it. I basically performed a preoder tree traversal back up the hierarchy to all records until father_id was NULL, which makes Step 03 stop collecting father_ids.

Give it a Try !!!

UPDATE 2011-12-16 12:18 EDT

Back on October 24, 2011, I actually wrote an algorithm in the MySQL Stored Procedure language to perform preorder tree traversal: Find highest level of a hierarchical field: with vs without CTEs . Hope it helps !!!

share|improve this answer
    
Thanks, let me try to implement this code –  Ashok KS Dec 15 '11 at 4:58

You may want to restructure the content of the database or optimize your queries to increase performance.

If your database is very large, then it is time to think about scaling out your database. Some of the things you could start looking at are:

  • Sharding
  • Partitioning
  • Replication - Master for writes, Slaves for reads.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.