MySQL - Slow query - mysql

Sorry for the long post, but the tables involved are quite big.
When I run the query bellow it normally takes about 1m to run. However, when I remove the correlated sub-query I get the query down to 15 seconds. So I think that's the actual problem.
Problem is, I don't really know how to get the SUM results without the correlated query.
QUERY I'm trying to run
SELECT cl_clients.vat as association_vat, cl_clients.name as association_name, cl_clients_with_regions.city_id, cl_clients_with_regions.nut_1, cl_clients_with_regions.nut_2, cl_clients_with_regions.nut_3,
company.vat as company_vat, company.name, company.section, company.division, company.cae,
SUM((
SELECT SUM((COALESCE((cl_finances.sales_community_market), 0) + COALESCE((cl_finances.sales_extra_market), 0)))
from cl_finances
where cl_finances.vat = company.vat
and cl_finances.year = 2018
)) as total_sum
FROM `cl_clients_with_regions`
JOIN `cl_client_intervention_areas` ON `cl_client_intervention_areas`.`city_id` = `cl_clients_with_regions`.`city_id`
JOIN `cl_clients` ON `cl_clients`.`vat` = `cl_client_intervention_areas`.`client_vat`
INNER JOIN cl_clients_with_regions company ON cl_clients_with_regions.vat = company.vat
WHERE `cl_clients_with_regions`.`encrypted_vat` IS NOT NULL
AND `cl_clients_with_regions`.`country` IS NOT NULL
AND `cl_clients_with_regions`.`cae` IS NOT NULL
AND `cl_clients_with_regions`.`cae` != ''
AND `cl_clients_with_regions`.`division` IS NOT NULL
AND `cl_clients_with_regions`.`section` = 'A'
AND `cl_clients_with_regions`.`nut_2` = 'Centro'
GROUP BY association_vat;
CL Clients create table syntax
create table cl_clients
(
vat varchar(20) default '' not null,
encrypted_vat varchar(32) null,
temporary_vat enum ('0', '1', '') default '0' null,
sig_id int null,
phc_id int null,
client_manager int null,
name varchar(300) null,
brand varchar(255) null,
`group` varchar(200) null,
class_internacional varchar(45) null,
logoimage varchar(400) null,
social_capital varchar(100) null,
address varchar(300) null,
gps varchar(25) null,
parish varchar(70) null,
zip_code varchar(10) null,
city int null,
district int null,
country int default 75 not null,
headquarter int null,
person_title varchar(7) null,
person_contact varchar(350) null,
person_phone varchar(20) null,
person_cell varchar(15) null,
person_email varchar(150) null,
person_function varchar(150) null,
language varchar(10) null,
phone varchar(20) null,
mobile_phone varchar(20) null,
fax varchar(20) null,
email varchar(50) null,
email_alternative varchar(50) null,
website varchar(300) null,
pme varchar(500) null,
pme_lider varchar(500) null,
cae varchar(15) null,
cae_2 varchar(15) null,
certified enum ('1', '0', '') null,
sector varchar(350) null,
workers int null,
foundation_date date null,
size enum ('Micro Empresa', 'Pequena empresa', 'Média empresa', 'Grande empresa', 'Não aplicável', 'PME', '') default '' null,
company_type varchar(455) default 'Empresa' null,
type enum ('novo', 'antigo', 'analizar', '') default 'analizar' null,
entity enum ('client', 'prospect', 'suplier', 'potential', '') default 'prospect' null,
client_to_country int null,
partner enum ('1', '0', '') default '0' not null,
partner_dp enum ('0', '1', '') default '0' null,
lucrative enum ('1', '0', '') default '1' not null,
dun varchar(100) null,
debt enum ('50k', '100k', '500k', '1m', '5m', '+5m', '') null,
bankruptcy enum ('1', '0', '') default '0' null,
competitors enum ('1', '0', '') default '0' null,
dun_date date null,
bank_1 varchar(30) null,
bank_2 varchar(30) null,
bank_3 varchar(30) null,
status enum ('0', '1', '') default '1' null,
followup enum ('0', '1', '') default '1' null,
classification enum ('Prestige', 'Premium', 'Current', '') default 'Current' null,
associated enum ('0', '1', '') default '0' null,
industry_id int null,
valid enum ('0', '1', '') default '1' null,
imported_at timestamp null,
modified_by int default 0 null,
responsible_id int null,
partner_type int null,
iberinform_id int null,
iberinform_date timestamp null,
legal_form text null,
going_concern text null,
score int null,
sector_score int null,
tax_status text null,
irc_debtors_ledger enum ('0', '1') null,
ss_debtors_ledger enum ('0', '1') null,
association_class_id int null,
nature enum ('business', 'commercial', 'sectorial', 'other') null,
geographic_scope enum ('national', 'regional', 'local') null,
intervention_nut enum ('nut_1', 'nut_2', 'nut_3') null,
ch_is_associated enum ('0', '1') null,
bi enum ('0', '1') default '0' null,
updated_at timestamp null,
constraint encrypted_vat_UNIQUE
unique (encrypted_vat),
constraint vat
unique (vat),
constraint cl_clients_cl_caes_cae_fk
foreign key (cae) references cl_caes (cae)
on update cascade,
constraint clients_industry_id_fk
foreign key (industry_id) references cl_industries (id),
constraint cltns_asso_clss_id_fk
foreign key (association_class_id) references cl_association_classifications (id)
on update cascade on delete set null,
constraint country_fk_id
foreign key (country) references cl_countries (id)
on update cascade on delete cascade
);
create index cl_client_name_idx
on cl_clients (name);
create index cl_clients_bi_idx
on cl_clients (bi);
create index cl_clients_brand_idx
on cl_clients (brand);
create index cl_clients_company_type_idx
on cl_clients (company_type);
create index cl_clients_geo_index
on cl_clients (city, district);
create index cl_clients_geographic_scope_idx
on cl_clients (geographic_scope);
create index cl_clients_group_idx
on cl_clients (`group`);
create index cl_clients_nature_idx
on cl_clients (nature);
create index cltns_asso_clss_id_fk_idx
on cl_clients (association_class_id);
create index country_idx
on cl_clients (country);
create index fk_clients_industries1_idx
on cl_clients (industry_id);
alter table cl_clients
add primary key (vat);
VIEW being used in the query
create view cl_clients_with_regions as
select `grupoch`.`cl_clients`.`vat` AS `vat`,
`grupoch`.`cl_clients`.`encrypted_vat` AS `encrypted_vat`,
`grupoch`.`cl_clients`.`name` AS `name`,
`grupoch`.`cl_clients`.`brand` AS `brand`,
`grupoch`.`cl_clients`.`country` AS `country`,
`grupoch`.`cl_clients`.`district` AS `district`,
`grupoch`.`cl_clients`.`city` AS `city`,
`grupoch`.`cl_cities`.`id` AS `city_id`,
`grupoch`.`cl_cities`.`name` AS `city_name`,
`grupoch`.`cl_cities`.`nut_1` AS `nut_1`,
`grupoch`.`cl_cities`.`nut_2` AS `nut_2`,
`grupoch`.`cl_cities`.`nut_3` AS `nut_3`,
`grupoch`.`cl_clients`.`cae` AS `cae`,
`grupoch`.`cl_caes`.`description` AS `cae_designation`,
`grupoch`.`cl_caes_divisions`.`division` AS `division`,
`grupoch`.`cl_caes_divisions`.`division_designation` AS `division_designation`,
`grupoch`.`cl_caes_divisions`.`section` AS `section`,
`grupoch`.`cl_caes_divisions`.`section_designation` AS `section_designation`
from (((`grupoch`.`cl_clients` join `grupoch`.`cl_cities` on ((
(`grupoch`.`cl_clients`.`city` = `grupoch`.`cl_cities`.`city_cod`) and (`grupoch`.`cl_clients`.`district` =
`grupoch`.`cl_cities`.`district_id`)))) join `grupoch`.`cl_caes` on ((`grupoch`.`cl_caes`.`cae` = `grupoch`.`cl_clients`.`cae`)))
join `grupoch`.`cl_caes_divisions`
on ((`grupoch`.`cl_caes_divisions`.`division` = `grupoch`.`cl_caes`.`division_id`)))
where ((`grupoch`.`cl_clients`.`country` in (75, 185, 186)) and (`grupoch`.`cl_clients`.`vat` is not null));
CL finances create table syntax
create table cl_finances
(
vat varchar(20) not null,
year int not null,
workers int null,
sells varchar(300) null,
sells_variation varchar(300) null,
international_sells varchar(300) null,
international_sells_variation varchar(300) null,
liquid_results varchar(300) null,
liquid_results_variation varchar(300) null,
capital varchar(300) null,
active_liquid varchar(300) null,
financial_autonomy varchar(300) null,
modified_by int default 0 null,
sales_profitability decimal(13, 2) null,
return_on_capital decimal(13, 2) null,
sales_community_market decimal(13, 2) null,
sales_extra_market decimal(13, 2) null,
created_at timestamp null,
updated_at timestamp null,
primary key (vat, year),
constraint clients_finances_vat
foreign key (vat) references cl_clients (vat)
on update cascade on delete cascade
);
create index cl_finances_year_idx
on cl_finances (year);
create index vat
on cl_finances (vat);
## Create table syntax for cl_client_intervention_areas
create table cl_client_intervention_areas
(
client_vat varchar(20) not null,
city_id int not null,
constraint fk_cl_client_intervention_areas_cl_cities1
foreign key (city_id) references cl_cities (id)
on update cascade on delete cascade,
constraint fk_cl_client_intervention_areas_cl_clients1
foreign key (client_vat) references cl_clients (vat)
on update cascade on delete cascade
);
create index fk_cl_client_intervention_areas_cl_cities1_idx
on cl_client_intervention_areas (city_id);
create index fk_cl_client_intervention_areas_cl_clients1_idx
on cl_client_intervention_areas (client_vat);
The explain result from this query is quite big

I believe the subquery is being executed once per row in the group by clause.
Could you try the following if it makes it any better.
Change i have done is to perform the correlated query as a inline-view and then join with the main table/view -- company.vat
SELECT cl_clients.vat as association_vat, cl_clients.name as association_name, cl_clients_with_regions.city_id, cl_clients_with_regions.nut_1, cl_clients_with_regions.nut_2, cl_clients_with_regions.nut_3,
company.vat as company_vat, company.name, company.section, company.division, company.cae,
SUM(temp_val.sum_val) as total_sum
FROM `cl_clients_with_regions`
JOIN `cl_client_intervention_areas` ON `cl_client_intervention_areas`.`city_id` = `cl_clients_with_regions`.`city_id`
JOIN `cl_clients` ON `cl_clients`.`vat` = `cl_client_intervention_areas`.`client_vat`
INNER JOIN cl_clients_with_regions company ON cl_clients_with_regions.vat = company.vat
LEFT JOIN (SELECT cl_finances.vat
,SUM((COALESCE((cl_finances.sales_community_market), 0) + COALESCE((cl_finances.sales_extra_market), 0))) as sum_val
FROM cl_finances
WHERE 1=1
AND cl_finances.year = 2018
GROUP BY cl_finances.vat
)temp_val
ON company.vat=temp_val.vat
WHERE `cl_clients_with_regions`.`encrypted_vat` IS NOT NULL
AND `cl_clients_with_regions`.`country` IS NOT NULL
AND `cl_clients_with_regions`.`cae` IS NOT NULL
AND `cl_clients_with_regions`.`cae` != ''
AND `cl_clients_with_regions`.`division` IS NOT NULL
AND `cl_clients_with_regions`.`section` = 'A'
AND `cl_clients_with_regions`.`nut_2` = 'Centro'
GROUP BY association_vat;

To further explain the above answer:
Subqueries should never be in the select clause because they execute once per record that is returned / or calculated to return. This causes the query to get slower and slower as the database gets larger.
Instead, the subquery should be in the FROM clause, and the result of the subquery should be joined to and selected.

Related

How can i improve GROUP BY statement performance?

I have a big query that i need to optimize.
select game_id,
count(user_id),
count(distinct user_id),
provider_id,
aggregator,
country,
sum(sum_bets),
sum(sum_wins),
currency,
any_value(date)
from (select transactions.game_id as game_id,
transactions.user_id as user_id,
games.provider_id as provider_id,
games.aggregator as aggregator,
users.country as country,
transactions.currency as currency,
IF(transactions.type_id IN (2, 16), transactions.amount, 0) as sum_bets,
IF(transactions.type_id IN (3, 18, 20), transactions.amount, 0) as sum_wins,
transactions.date_create as date
from `transactions`
inner join `games` on transactions.game_id = games.id
inner join `users` on transactions.user_id = users.id
where transactions.status_id = 1
and transactions.type_id in (2, 3, 16, 18, 20)
and transactions.game_id is not null
and users.country is not null
union all
select transactions_bonus.game_id as game_id,
transactions_bonus.user_id as user_id,
games.provider_id as provider_id,
games.aggregator as aggregator,
users.country as country,
transactions_bonus.currency as currency,
IF(transactions_bonus.type IN (5, 6), transactions_bonus.amount_bonus, 0) as sum_bets,
IF(transactions_bonus.type IN (4, 7, 10), transactions_bonus.amount_bonus, 0) as sum_wins,
transactions_bonus.date as date
from `transactions_bonus`
inner join `games` on transactions_bonus.game_id = games.id
inner join `users` on transactions_bonus.user_id = users.id
where transactions_bonus.status in (2, 3, 4, 5, 6)
and transactions_bonus.type in (4, 5, 6, 7, 10)
and transactions_bonus.game_id is not null
and users.country is not null) as popular_games
group by game_id, provider_id, aggregator, country, currency
Table transactions
-- auto-generated definition
create table transactions
(
id bigint unsigned auto_increment
primary key,
string_id varchar(36) null,
user_id bigint unsigned not null,
game_id smallint unsigned null,
bonus_id smallint unsigned null,
jackpot_id bigint unsigned null,
prize_id bigint unsigned null,
tournament_id bigint unsigned null,
amount int not null,
commission int null,
aggregator_amount int unsigned default '0' not null,
aggregator_currency varchar(16) collate utf8_bin null,
aggregator_fee int unsigned default '0' not null,
currency varchar(3) not null,
status_id tinyint unsigned not null,
balance int null,
gateway_method varchar(64) null,
account varchar(128) collate utf8_bin null comment 'Аккаунт в платежной системе (номер карты, телефон, кошелек)',
ip varchar(39) null,
comment varchar(128) null,
date_create datetime default CURRENT_TIMESTAMP not null,
date_complete datetime null,
date_cancel datetime null,
type_id tinyint unsigned not null,
manager_id int null,
event_id int null,
constraint transactions_ibfk_1
foreign key (user_id) references users (id)
on update cascade,
constraint transactions_ibfk_2
foreign key (game_id) references games (id)
on update cascade,
constraint transactions_ibfk_3
foreign key (jackpot_id) references jackpots (id)
on update cascade,
constraint transactions_ibfk_4
foreign key (tournament_id) references tournaments (id)
on update cascade,
constraint transactions_prize_id_foreign
foreign key (prize_id) references prizes (id)
on update cascade
)
engine = InnoDB;
create index date_create
on transactions (date_create);
create index game_id
on transactions (game_id);
create index jackpot_id
on transactions (jackpot_id);
create index string_id
on transactions (string_id);
create index tournament_id
on transactions (tournament_id);
create index type_id
on transactions (type_id);
create index user_id
on transactions (user_id);
Table transactions_bonus
-- auto-generated definition
create table transactions_bonus
(
id bigint unsigned auto_increment
primary key,
account tinyint unsigned default '1' not null comment 'Номер счета. 1 - бонусный счет казино, 2 - бонусный счет спорта',
type tinyint unsigned not null,
user_id bigint unsigned not null,
admin_id bigint unsigned null,
transaction_id bigint unsigned null,
bonus_id bigint unsigned null,
tournament_id bigint unsigned null,
prize_id bigint unsigned null,
game_id smallint unsigned null,
amount_bonus int not null,
amount_deposit int null,
currency varchar(3) not null,
wager float not null,
max_bet int unsigned null,
max_transfer int unsigned default '0' not null comment 'Максимальная сумма перевода с бонусного счета на основной',
balance int not null,
status tinyint unsigned not null,
comment text null,
date timestamp default CURRENT_TIMESTAMP not null,
date_cancel timestamp null,
date_wagering_complete timestamp null,
constraint transactions_bonus_ibfk_1
foreign key (admin_id) references users (id)
on update cascade,
constraint transactions_bonus_ibfk_3
foreign key (tournament_id) references tournaments (id)
on update cascade,
constraint transactions_bonus_ibfk_4
foreign key (user_id) references users (id)
on update cascade,
constraint transactions_bonus_ibfk_5
foreign key (game_id) references games (id)
on update cascade,
constraint transactions_bonus_ibfk_6
foreign key (prize_id) references prizes (id)
on update cascade,
constraint transactions_bonus_ibfk_7
foreign key (bonus_id) references bonuses (id)
on update cascade,
constraint transactions_bonus_ibfk_8
foreign key (transaction_id) references transactions (id)
on update cascade
)
engine = InnoDB;
create index account
on transactions_bonus (account);
create index admin_id
on transactions_bonus (admin_id);
create index bonus_id
on transactions_bonus (bonus_id);
create index game_id
on transactions_bonus (game_id);
create index status
on transactions_bonus (status);
create index tournament_id
on transactions_bonus (tournament_id);
create index transaction_id_2
on transactions_bonus (transaction_id);
create index type
on transactions_bonus (type);
create index user_id
on transactions_bonus (user_id);
Table games
-- auto-generated definition
create table games
(
id smallint unsigned auto_increment
primary key,
string_id varchar(96) collate utf8_bin not null,
provider_id tinyint unsigned not null,
aggregator tinyint unsigned not null,
name varchar(255) not null,
game_type tinyint unsigned not null,
source tinyint unsigned not null,
technology tinyint unsigned not null,
device_type tinyint unsigned not null,
status tinyint unsigned default '0' not null,
block_kyc tinyint unsigned default '0' not null comment 'Блокировка для не верифицированных пользователей',
block_jackpots tinyint unsigned default '0' not null comment 'Не участвует в джекпотах',
is_blocked_tournaments tinyint unsigned default '0' not null,
is_wagering tinyint default 0 null,
is_bonuses tinyint unsigned default '0' null,
is_jackpots tinyint unsigned default '0' not null,
freespins tinyint unsigned default '0' not null comment 'Участвует в фриспинах',
has_demo tinyint(1) default 0 not null,
multiplier decimal(8, 2) unsigned default 0.00 not null,
image varchar(128) null,
sorting int unsigned default '0' not null,
created_at timestamp default CURRENT_TIMESTAMP null,
updated_at timestamp null,
blocked smallint unsigned default '0' null,
constraint string_id
unique (string_id, aggregator),
constraint games_ibfk_1
foreign key (provider_id) references games_providers (id)
)
engine = InnoDB;
create index provider_id
on games (provider_id);
Table users
-- auto-generated definition
create table users
(
id bigint unsigned auto_increment
primary key,
name varchar(255) charset utf8 null,
surname varchar(255) null,
email varchar(255) null,
phone varchar(12) null,
password varchar(255) not null,
user_group tinyint(1) not null,
status tinyint(1) default 0 not null,
network varchar(32) null,
network_uid varchar(32) null,
login varchar(255) not null,
ip_registration varchar(39) not null,
ip_auth varchar(39) null,
country varchar(3) charset utf8 null,
country_already_changed tinyint unsigned default '0' not null comment 'Страну можно изменить только один раз',
birthday_already_changed tinyint unsigned default '0' not null comment 'Дату рождения можно установить один раз',
city varchar(255) null,
postcode varchar(15) null,
date_birth date null,
date_registration timestamp default CURRENT_TIMESTAMP not null,
date_auth timestamp null,
date_last_activity timestamp null,
comment varchar(265) null,
affiliate_id varchar(32) charset utf8 null,
affiliate_user_id varchar(255) null,
affiliate_company_id varchar(32) null,
affiliate_payload varchar(192) charset utf8 null,
affiliate_link_type tinyint unsigned default '0' not null comment 'Тип партнерской ссылки, по которой перешел юзер',
favorite_bets tinyint unsigned default '0' not null comment 'Любимые ставки игрока',
timezone varchar(64) charset utf8 null,
region varchar(255) null,
address varchar(265) null,
kyc_status tinyint unsigned default '1' not null,
role_id tinyint null,
sms_autentification tinyint unsigned default '0' not null,
remember_token varchar(100) null,
created_at timestamp default CURRENT_TIMESTAMP not null,
updated_at timestamp null,
kyc_expire date null,
sex varchar(1) charset utf8 default 'm' null,
login_attempt int default 0 not null,
language varchar(2) charset utf8 default 'en' not null,
session_uuid varchar(36) null,
session_token varchar(64) null,
avatar varchar(265) null,
referral_user_id bigint unsigned null,
level tinyint unsigned default '0' not null,
points int unsigned default '0' not null,
shop_points int unsigned default '0' not null,
last_points_used timestamp null,
max_withdrawal_amount int unsigned null,
promocode_error_attempts int unsigned default '0' null,
promocode_block_to datetime null,
bonus_blocked tinyint unsigned default '0' not null comment 'Заблокирован для участия в бонусах',
bonus_blocked_date timestamp null,
blocked tinyint unsigned default '0' not null comment 'Блокировка Sumsub',
service_status varchar(32) null comment 'Статус сервиса SumSub',
constraint login
unique (login),
constraint network
unique (network, network_uid),
constraint users_email_unique
unique (email),
constraint users_ibfk_1
foreign key (referral_user_id) references users (id)
on update cascade
)
engine = InnoDB
collate = utf8mb4_unicode_ci;
create index date_last_activity
on users (date_last_activity);
create index referral_user_id
on users (referral_user_id);
These two merged tables have combined 30 million rows, without GROUP BY statement, it executes within a 1.5-2 seconds. With GROUP BY, i can wait up to 5 minutes until it executes.
Obviously, i can execute script without GROUP BY, and then format and aggregate it in my code.
But so many records will breach the limit of my RAM, if i will save it in array or JSON.
Be aware that count(user_id) counts the number of rows that have user_id IS NOT NULL. It seems like it won't be NULL, so you may as well do COUNT(*).
In older versions ANY_VALUE(date) does not work but MIN(date) works.
There are lot of IN(...) lists; this makes it virtually impossible to fashion any useful INDEXes. Here are some that could help:
transactions: INDEX(status_id, type_id)
If it weren't for COUNT(DISTINCT user_id), I would recommend doing the SUMs in each subquery, then adding the subtotals.
What is the value of innodb_buffer_pool_size? How much RAM? How big are the tables (in GB)? If things can't be cached adequately, then I/O is the problem and I will suggest changing BIGINTs (8 bytes each) to more civilized datatypes.
"two merged tables have combined 30 million rows" -- Is it 30M before or after filtering (WHERE)?
Please provide EXPLAIN SELECT ...

Duplicate entry ' ' for key '...PRIMARY'

I am using mysql workbench 8.0 CE.
The table:
CREATE TABLE IF NOT EXISTS `Table`.`staff` (
`StaffNumber` INT NOT NULL,
`NameFirst` VARCHAR(45) NULL,
`NameLast` VARCHAR(45) NULL,
`AdressStreet` VARCHAR(45) NULL,
`PostalArea` VARCHAR(45) NULL,
`PostalCode` INT NULL,
`tlf` INT NULL,
`Birth` VARCHAR(45) NULL,
`sex` VARCHAR(45) NULL,
`SSN` INT NULL,
`position` VARCHAR(45) NULL,
`Salary` FLOAT NULL,
`Veterinary_Clinics_Clinicnumber` INT NOT NULL,
PRIMARY KEY (`StaffNumber`),
INDEX `fk_Staff_Veterinary_Clinics_idx` (`Veterinary_Clinics_Clinicnumber` ASC) VISIBLE,
CONSTRAINT `fk_Staff_Veterinary_Clinics`
FOREIGN KEY (`Veterinary_Clinics_Clinicnumber`)
REFERENCES `Table`.`Veterinary_Clinics` (`Clinicnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;`
And it contains this:
insert into `Table`.`staff`(
`StaffNumber`,
`NameFirst`,
`NameLast`,
`AdressStreet`,
`PostalArea`,
`PostalCode`,
`tlf`,
`Birth`,
`sex`,
`SSN`,
`position`,
`Salary`,
`Veterinary_Clinics_Clinicnumber`
)
values
( 12, 'Name', 'LastName', 'Street', 'Place', 4217, 41354607,
'22.05.1976', 'Woman', 2, 'Doctor', 5000.00, 1
);
When I execute this, I get the error "Duplicate entry '12' for 'staff.PRIMARY'". I do not have two of the same values in the same column, and the values matches, so I cant see where the problem is... can someone help me out.

#1062 - Duplicate entry '0' for key 'PRIMARY' 7

Getting this error and not being savvy with creating tables and columns some of the other similar problems and answers haven't helped.
This is the error:
Error
SQL query:
INSERT INTO orders_session_info
(txn_signature , orders_id , payment , payment_title , payment_amount , payment_currency , payment_currency_val , sendto , billto , language , language_id , currency , currency_value , firstname , lastname , content_type , affiliate_id , affiliate_date , affiliate_browser, affiliate_ipaddress , affiliate_clickthroughs_id )
VALUES
(
'd4208b15558a761c34dc7c79b87275b4', '20167', 'paypal', 'PayPal', '273.56', 'USD', '1.00000000', '140', '140', 'english', '1', 'USD', '1.00000000', 'Pauls', 'Dandzbergs', 'physical', '0', '0000-00-00 00:00:00', '', '', '0'
);
MySQL said: Documentation
#1062 - Duplicate entry '0' for key 'PRIMARY'
And the code:
-- Table structure for table `orders_session_info`
--
DROP TABLE IF EXISTS orders_session_info;
CREATE TABLE orders_session_info (
txn_signature int(11) NOT NULL AUTO_INCREMENT,
orders_id int(11) NOT NULL,
payment varchar(32) NOT NULL,
payment_title varchar(32) NOT NULL,
payment_amount varchar(32) NOT NULL,
payment_currency varchar(32) NOT NULL,
payment_currency_val varchar(32) NOT NULL,
sendto varchar(32) NOT NULL,
billto varchar(32) NOT NULL,
language varchar(32) NOT NULL,
language_id varchar(32) NOT NULL,
currency varchar(32) NOT NULL,
currency_value varchar(32) NOT NULL,
firstname varchar(32) NOT NULL,
lastname varchar(128) NOT NULL,
content_type varchar(128) NOT NULL,
affiliate_id varchar(128) NOT NULL,
affiliate_date varchar(128) NOT NULL,
affiliate_browser varchar(128) NOT NULL,
affiliate_ipaddress varchar(128) NOT NULL,
affiliate_clickthroughs_id varchar(128) NOT NULL,
PRIMARY KEY (txn_signature),
KEY txn_signature(orders_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
any specific help would be much appreciated...
--
-- Dumping data for table orders_session_info
Your primary key field txn_signature is an auto increment field. This means the database will create a unique value for it automatically when you insert a new row.
Therefore you should not include this field in your INSERT statement - the database will take care of it for you. Right now it thinks you're trying to override the automatic functionality and insert a value of "d4208b15558a761c34dc7c79b87275b4" manually. Obviously this would result in duplicate IDs once you have more than one row, so it's not allowed. Also, your field is an int and you're trying to insert a string into it, which will never work either.

No auto Increment when MySql Database converted to Postgres database

In Mysql, ID is Auto Increment but when converted to Postgres there is no Auto Increment .
Mysql database
CREATE TABLE IF NOT EXISTS `cities` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`state_id` bigint(20) NOT NULL,
`district_id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(1000) DEFAULT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) DEFAULT NULL,
`is_active` char(1) NOT NULL DEFAULT 'Y',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `state_id` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
After Converted to postgres database
CREATE TABLE cities (
"id" bigint NOT NULL,
state_id bigint NOT NULL,
district_id bigint NOT NULL,
"name" varchar(255) NOT NULL,
description varchar(1000),
created_by int NOT NULL,
modified_by int,
is_active char(1) NOT NULL,
created timestamp,
modified timestamp,
PRIMARY KEY ("id")
);
INSERT INTO cities(state_id, district_id, city_type, name, description,
created_by, modified_by, is_active, created, modified)
VALUES
(1, 1, '', 'Ramtek', null, 1, null, 'Y',
'2015-04-16 10:44:11', '2015-04-16 10:44:11');
You could use bigserial in such cases. It provides an auto increment feature in postgres:
CREATE TABLE cities (
"id" bigserial PRIMARY KEY,
Not Null constraint is provided by default.
Docs : http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-SERIAL

Unexpected behaviour of the query

This particular query is supposed to enter the user into the database.
This query does not always insert the values of the firstname and the lastname fields along with others. firstname and lastname are empty for a few insertions and for the others its working as expected.
INSERT INTO `users` (mobile, passwordHash, firstname, lastname, ent_id, email )
VALUES ('913800341127', '678a1491514b7f1006d605e9161946b1', 'nat', 'sam', '108', NULL)
ON DUPLICATE KEY UPDATE `firstname` = VALUES(firstname),`lastname` = VALUES(lastname)
Related Info:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`tag` varchar(5) NOT NULL default 'ind',
`username` varchar(50) default NULL,
`firstname` varchar(100) default NULL,
`lastname` varchar(100) default NULL,
`passwordhash` varchar(255) NOT NULL,
`secretq` varchar(255) default NULL,
`secreta` varchar(100) default NULL,
`email` varchar(50) default NULL,
`mobile` varchar(13) default NULL,
`last_login` datetime default NULL,
`ent_id` bigint(20) NOT NULL default '1',
`is_inactive` tinyint(1) NOT NULL COMMENT 'Whether the user is active or not',
PRIMARY KEY (`id`),
UNIQUE KEY `mobile_2` (`mobile`,`ent_id`),
UNIQUE KEY `email_2` (`email`,`ent_id`),
KEY `username` (`username`),
KEY `ent_id` (`ent_id`,`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
syntax error it should be :
ON DUPLICATE KEY UPDATE firstname = 'nat',lastname = 'sam'