A table causes the site to be slow - mysql

there is this table in my project that makes my site be very slow.
The table stored follower/followings data, and currently has 2783 records. When I empty this table? the site regains its speed.
this is the structure of the table from mysqldumps:
CREATE TABLE IF NOT EXISTS `followers` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`active` int(1) NOT NULL DEFAULT '1',
`is_chatting` int(1) NOT NULL DEFAULT '0',
`follower_id` int(255) NOT NULL,
`following_id` int(255) NOT NULL,
`time` int(255) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3283 ;
May someone please help me figure this out.

Related

Prevent MySQL from duplicates per given project

I have this MySQL table
CREATE TABLE `d_hits` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`projectId` varchar(36) NOT NULL,
`data` text CHARACTER SET utf8,
`extras` text,
`status` varchar(50) NOT NULL DEFAULT 'notDone',
`evaluation` varchar(50) DEFAULT 'NONE',
`isGoldenHIT` tinyint(1) DEFAULT '0',
`goldenHITResultId` int(11) unsigned DEFAULT '0',
`notes` text,
`created_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isURL` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3561574 DEFAULT CHARSET=latin1;
My goal here is to prevent the database from creating duplicates data for a given project. For example, project with projectID: 123 has data: link1 but if I enter data: link1 again it should prevent it from entering. However if projectID is 333 and the given data is again link1, it should insert it without any problems. My question is, how can I prevent the duplicates per project?
You seem to want a unique constraint.
ALTER TABLE d_hits
ADD UNIQUE (projectid,
data);

Mysql - Cannot add foreign key constraint, there is no forign key in SQL query

This question is completely different from similar ones. There is no foreign key in the SQL query. This is a silly error I see when I import the SQL file on remote server. This is the SQL code
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
As you see there is no foreign key, But when I run the following code, it is ok
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ;
If I rename it to something else it is OK too.
CREATE TABLE `locationssss` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
what is wrong?
Just for future references:
Do you have more tables within your database? If so, is there a table that does contain a foreign key connected with the locations table?

MySQL query error #1064?

I'm trying to import my old database but this gave me some errors what makes it impossible im also searching on google for 30 minuts and i can't find any solution?
SQL-query:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` datetime NOT NULL DEFAULT TIMESTAMP,
PRIMARY KEY (`id`), KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
MySQL meldt: Documentatie
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
PRIMARY KEY (`id`), KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT C' at line 6
The correct default value is CURRENT_TIMESTAMP:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
The SQL Fiddle is here.
EDIT:
You must be using an old-ish version of MySQL (okay, not that old, just pre-5.6). Well, you can't default a datetime value (without a trigger), so you have to live with a TIMESTAMP value and learn to love the timestamp functions:
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
First solution : change datatype into timestamp like this
CREATE TABLE `UG_blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ownerid` int(11) NOT NULL,
`content` text NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY id(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
The problem is : The Timestamp data type has a range of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07.
So the second solution is, don't change datetime datatype into timestamp. But you need a trigger to set default value.

MySql - Create view to read from Multiple Tables

I have archived some old line items for invoices that are no longer current but still need to reference them. I think I need to create a VIEW but not really understanding it. Can someone help so I can run a query to pull the invoice and then the total of all the line items assigned (no matter what table the items are in)?
CREATE TABLE `Invoice` (
`Invoice_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`Invoice_CreatedDateTime` DATETIME DEFAULT NULL,
`Invoice_Status` ENUM('Paid','Sent','Unsent','Hold') DEFAULT NULL,
`LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `LastUpdatedAt` (`LastUpdatedAt`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1
CREATE TABLE `Invoice_LineItem` (
`LineItem_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`LineItem_ChargeType` VARCHAR(64) NOT NULL DEFAULT '',
`LineItem_InvoiceID` INT(11) UNSIGNED DEFAULT NULL,
`LineItem_Amount` DECIMAL(11,4) DEFAULT NULL,
`LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`LineItem_ID`),
KEY `LastUpdatedAt` (`LastUpdatedAt`),
KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
CREATE TABLE `Invoice_LineItem_Archived` (
`LineItem_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`LineItem_ChargeType` VARCHAR(64) NOT NULL DEFAULT '',
`LineItem_InvoiceID` INT(11) UNSIGNED DEFAULT NULL,
`LineItem_Amount` DECIMAL(11,4) DEFAULT NULL,
`LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`LineItem_ID`),
KEY `LastUpdatedAt` (`LastUpdatedAt`),
KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
Typically I would just run the following query to get the amount due on the invoices
SELECT
Invoice_ID,
Invoice_CreatedDateTime,
Invoice_Status,
(SELECT SUM(LineItem_Amount) AS totAmt FROM Invoice_LineItem WHERE LineItem_InvoiceID=Invoice_ID) AS Invoice_Total
FROM
Invoice
WHERE
Invoice_Status='Sent'
Also how can I select all the line items from both tables in one query?
SELECT
LineItem_ID,
LineItem_ChargeType,
LineItem_Amount
FROM
Invoice_LineItem
WHERE
LineItem_InvoiceID='1234'
You can use the MERGE Storage Engine to create a virtual table that's the union of two real tables:
CREATE TABLE Invoice_LineItem_All
(
`LineItem_ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`LineItem_ChargeType` VARCHAR(64) NOT NULL DEFAULT '',
`LineItem_InvoiceID` INT(11) UNSIGNED DEFAULT NULL,
`LineItem_Amount` DECIMAL(11,4) DEFAULT NULL,
`LastUpdatedAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
KEY (`LineItem_ID`),
KEY `LastUpdatedAt` (`LastUpdatedAt`),
KEY `LineItem_InvoiceID` (`LineItem_InvoiceID`)
) ENGINE=MERGE UNION=(Invoice_LineItem_Archived, Invoice_LineItem);
You can use UNION :
SELECT a.* FROM a
UNION
SELECT b.* FROM b;
You just need to have the same number and type of column in your different queries.
As far as I remember, you can add test in sub-queries, but I'm not sure you can order on the global result.
http://dev.mysql.com/doc/refman/4.1/en/union.html

Mysql Append table to add columns

I like to append a table to add column but without using alert table command
e.g.
This is the table which is missing some columns.
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
So if i run this query then it should automatically add missing columns into my tables
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`name` varchar(100) NOT NULL,
`originalUser` tinyint(1) NOT NULL default '0',
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Can this be possible to do without using alert table command ?
I understand your question as you want to add some columns to your table. Please be informed that the term row is usually related to the actual data in your table, not the columns itself. If my assumption is wrong, please clarify your question.
You cannot use CREATE TABLE for altering a table. It is there to create table,
and if it cannot create it, it will in most cases throw an error like you described. Another command exists for that reason: ALTER TABLE.
You might do it something like this.
(1) Create your table with your CREATE TABLE syntax above:
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`passwd` varchar(40) NOT NULL,
`isActive` tinyint(1) NOT NULL default '1',
`lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
(2) Use ALTER TABLE like this to make the modifications I think you want to have in your second statement (two more columns):
ALTER TABLE
ADD COLUMN `name` varchar(100) NOT NULL AFTER `passwd`,
ADD COLUMN `originalUser` tinyint(1) NOT NULL default '0' AFTER `name`;
Not related to your question, but I'd avoid column names like name, because if you don't escape them properly it'll throw you other errors (see reserved words).