|
Execution plan is depending on multiple factors.
How to optimize?
Get rid of excess colunms, get red of the date-constriction, get rid of region. Best idea; find a damn programmer.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I am getting dead lock issue due to below transaction. Please could you help on this.
------------------------------------------------------Transaction 1------------------------------------------------------
Transactionname="DELETE"
waitresource="KEY: 6:72057594046054400 (6a6c767d8e74)"
lasttranstarted="2020-07-11T05:48:38.007"
lockMode="U"
isolationlevel="read committed (2)"
status="suspended"
DELETE FROM [Channel] WHERE FileInventoryKey IN (SELECT FileInventoryKey FROM #FileInventoryKeys)
--------------------------------------------------------Transaction 2------------------------------------------------------
Transactionname="UPDATE"
waitresource="KEY: 6:72057594046054400 (93201dea6b78)"
lasttranstarted="2020-07-11T05:48:38.007"
lockMode="U"
isolationlevel="read committed (2)"
status="suspended"
update ch set ChannelKey = c.ChannelKey
from Channel ch
join ChannelVersion c on c.ChannelID = ch.ChannelID
join Channel cc on cc.ChannelKey = c.ChannelKey and (cc.HasIntervalData = 0 and ch.IsRegister = 'true') --register channels where c.ChannelVersionKey in (select ChannelVersionKey from [#UpdatedChannels]
I have two options
1). UPD lock on the update statement
2). SET TRANSACTION ISOLATION LEVEL Snapshot
|
|
|
|
|
If you are using SQL server and the DELETE operation can be delayed or postponed try this approach …
Use an application lock to synchronize two different processes that data is being manipulated and try again later ...
Microsoft has sp_getapplock
Maybe it would work for you.
|
|
|
|
|
These are three solutions which one will be the best one and let me know if i need to change anything in the solution.
First solution :
EXEC @returnCode = Sp_getapplock
@Resource = 'Resource_stg_SqlQueueManager',
@LockMode = 'Exclusive',
@LockOwner = 'Transaction ',
@LockTimeout = 500000
Second solution
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
Third solution
Update with nolock
Example :
update ch set ChannelKey = c.ChannelKey
from CRF_Channel ch with (nolock)
join ChannelVersion c on c.ChannelID = ch.ChannelID
join Channel cc on cc.ChannelKey = c.ChannelKey
and (cc.HasIntervalData = 0 and ch.IsRegister = 'true')
|
|
|
|
|
Hi, I have two columns in a table, one is Auto Identity set, and other one should be created as a string value with Id as Pad right, I have created a trigger but my lead is saying Triggers impact the performance, and to not create, is there any alternate for Triggers that would not effect Database performance at the same time do what I am expecting it to be done.
My trigger is as follows:
CREATE TRIGGER AfterInsert
ON InspectionItems
AFTER INSERT
AS
BEGIN
declare @InspectionItemNumber nvarchar(20), @InspectionItemId int
select @InspectionItemId = ins.InspectionItemId FROM INSERTED ins;
update InspectionItems set InspectionItemNumber = 'T' + RIGHT('00000'+CAST(InspectionItemId AS VARCHAR(5)),6)
where InspectionItemId = @InspectionItemId
END
GO
Is there any alternate for trigger and doing the same and preserve the performance - thanks in advance.
|
|
|
|
|
Use a computed column:
Specify Computed Columns in a Table - SQL Server | Microsoft Docs[^]
If you want to continue using a trigger, you'll need to update it to account for the fact that a trigger can be fired for multiple rows at the same time:
CREATE TRIGGER AfterInsert
ON InspectionItems
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE
T
SET
InspectionItemNumber = 'T' + RIGHT('00000' + CAST(InspectionItemId AS VARCHAR(5)), 6)
FROM
InspectionItems As T
INNER JOIN inserted As I
ON I.InspectionItemId = T.InspectionItemId
;
END
GO
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
The main application where I work creates and then destroys a Connection object for every stored procedure that it calls. (SQL Server 2012)
Is this unnecessarily wasteful to the point where it might speed things up to reuse open connections?
Or do you think it doesn't make much of a difference?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It's the recommended approach - create the connection as close as possible to where it's needed, and wrap it in a "using" block to make sure it's disposed of as soon as you're done with it.
Connection pooling will automatically keep a pool of the low-level connections alive to speed up reconnecting. So long as you're using the same connection string each time, and you dispose of the connection when you're done with it, you shouldn't have any performance issues caused by connecting to SQL.
SQL Server Connection Pooling - ADO.NET | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
HI Team,
I am facing deadlock issue in the SQL Server. I have two process using the same table at same time. first process will insert the records using bulk insert and second process will update the records in the same table.
Please could you let me know how can avoid deadlock.
|
|
|
|
|
|
Is it better to use tab lock on the table during the bulk insert?
|
|
|
|
|
Sorry, I don't know. Check the documentation to see what it recommends.
|
|
|
|
|
If you are using a script to initiate the bulk load, at the end of the script start the process for updating.
That is a simple way of synchronizing the processes.
|
|
|
|
|
Both process are different based to the file availability process will kick start. Monthly I am getting deadlock issue when both the files are available at same time by utilizing the same table.
As per my knowledge, I need to set lock/ Isolation level at store procedure to overcome this issue.
|
|
|
|
|
Do one then do the other.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Hi Could anyone help me with the following code? It was working fine until I inserted a username column in the DB table and edited this to suit (I thought) It gives an HTTP500 error page not working.
<?php
require_once "config.php";
$email = $password = $confirm_password = "";
$username_err = $username = $confirm_username_err = "";
$email_err = $password_err = $confirm_password_err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["email"]))){
$email_err = "Please enter your email.";
} else{
$sql = "SELECT id FROM users WHERE email = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_email);
$param_email = trim($_POST["email"]);
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$email_err = "This email is already registered.";
} else{
$email = trim($_POST["email"]);
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
mysqli_stmt_close($stmt);
}
}
if(empty(trim($_POST["username"]))){
$username_err = "Please enter a username.";
} elseif(strlen(trim($_POST["username"])) < 6){
$username_err = "username must have atleast 6 characters.";
} else{
$username = trim($_POST["username"]);
}
}
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password.";
} elseif(strlen(trim($_POST["password"])) < 6){
$password_err = "Password must have atleast 6 characters.";
} else{
$password = trim($_POST["password"]);
}
if(empty(trim($_POST["confirm_password"]))){
$confirm_password_err = "Please confirm password.";
} else{
$confirm_password = trim($_POST["confirm_password"]);
if(empty($password_err) && ($password != $confirm_password)){
$confirm_password_err = "Password did not match.";
}
}
if(empty($email_err) && empty($password_err) && empty($confirm_password_err)){
$sql = "INSERT INTO users (email, username, password) VALUES (?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "ss", $param_email,$param_username $param_password);
$param_email = $email;
$param_username = $username
$param_password = password_hash($password, PASSWORD_DEFAULT);
if(mysqli_stmt_execute($stmt)){
header("location: login.php");
} else{
echo "Something went wrong. Please try again later.";
}
mysqli_stmt_close($stmt);
}
}
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<body>
<div class="wrapper">
<h2>Sign Up</h2>
<p>Please fill this form to create an account.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
<?php echo $email_err; ?>
</div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
<?php echo $username_err; ?>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
<?php echo $password_err; ?>
</div>
<div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
<label>Confirm Password</label>
<input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
<?php echo $confirm_password_err; ?>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
<p>Already have an account? <a href="login.php">Login here</a>.</p>
</form>
</div>
</body>
</html>
|
|
|
|
|
500 is a generic error from the server. You'll need to debug it or get the details of the error.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Thanks for replying ZurdoDev.
I was thinking it had to be something in the prepared statement that I have done wrong but can't figure it out. I ran a query to create the table and columns so assume that must be correct?
|
|
|
|
|
Somewhere you should be able to get the details of the error. I don't do much php but if you aren't seeing the error when you run it then maybe look in the logs.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
and180y wrote:
$sql = "INSERT INTO users (email, username, password) VALUES (?, ?)";
...
mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_username $param_password); Two obvious problems:
You're inserting into three columns, but you've only specified two values:
$sql = "INSERT INTO users (email, username, password) VALUES (?, ?, ?)"; You've only specified types for two parameters, and you're missing a comma between the last two parameters:
mysqli_stmt_bind_param($stmt, "sss", $param_email, $param_username, $param_password);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks for replying Richard.
I've fixed those errors but still having the same issue, it loaded the form once but now just a white page with the error.
|
|
|
|
|
I have played around starting again from the basic code and adding in what I need and checking for errors, all is fine till I put the code below in.
}
if(empty(trim($_POST["email"]))){
$email_err = "Please enter your email.";
} elseif(strlen(trim($_POST["email"])) < 6){
$email_err = "email must have at least 6 characters.";
} else{
$email = trim($_POST["email"]);
}
|
|
|
|
|
if(empty(trim($_POST["email"]))){
If the email variable is blank then calling trim on it will fail. You should change it to:
if(empty($_POST["email"])){
|
|
|
|
|
Working perfect now. Thanks Richard.
|
|
|
|