Thursday, 29 February 2024

Dead lock in SQL

 SELECT *   FROM sys.dm_tran_active_transactions;


A deadlock is a situation in a database system where two or more transactions are waiting indefinitely for each other to release resources that they need to proceed. This creates a circular dependency, causing each transaction to be blocked from completing, resulting in a deadlock situation.

Here's a simplified explanation of how a deadlock can occur:

  1. Transaction A holds a lock on Resource X and needs a lock on Resource Y to complete.
  2. Transaction B holds a lock on Resource Y and needs a lock on Resource X to complete

Wednesday, 21 February 2024

Sunday, 18 February 2024

delete duplicate records based on the Id

 WITH CTE AS (

    SELECT ProductId,

           -- Add other columns as needed

           ROW_NUMBER() OVER (PARTITION BY ProductId ORDER BY (SELECT NULL)) AS RowNumber

    FROM Master.VehicleType

)

DELETE FROM CTE WHERE RowNumber > 1;

Tuesday, 13 February 2024

SQL ACID

 


Certainly! Let's consider a banking scenario to illustrate the ACID properties:

  1. Atomicity: Imagine a scenario where a bank customer wants to transfer money from one account to another. The transaction involves two steps: deducting money from the sender's account and adding it to the receiver's account. Atomicity ensures that either both of these steps are completed successfully, or none of them are. If, for example, the system deducts money from the sender's account but encounters an error before adding it to the receiver's account, atomicity ensures that the deduction is rolled back, leaving both accounts unchanged.

  2. Consistency: Suppose a bank has a rule that no account balance should go negative. If a transaction would violate this rule, it should not be allowed to proceed. Consistency ensures that the database remains in a consistent state before and after each transaction. So, if a transaction tries to deduct an amount that would result in a negative balance, it will be rejected to maintain consistency.

  3. Isolation: Consider a situation where multiple bank customers are simultaneously transferring money between their accounts. Isolation ensures that each transaction is executed as if it is the only transaction in the system. Even though multiple transactions are happening concurrently, they do not interfere with each other. This means that each transaction sees a consistent view of the data and does not observe the intermediate states of other transactions.

  4. Durability: After a successful money transfer transaction, the changes made to the account balances must persist, even if there's a system crash or power failure. Durability ensures that once a transaction is committed, the changes are permanently saved to the database and will not be lost, even in the event of a failure.

In summary, ACID properties ensure that transactions in a database system are reliable, maintain data integrity, allow concurrent execution without interference, and ensure that changes are durable and persist even in the face of failures.

Sunday, 11 February 2024

Optimizing the performance of a .NET Core Web API

 Optimizing the performance of a .NET Core Web API involves various strategies aimed at improving response times, reducing resource consumption, and enhancing scalability. Here are some key approaches to optimize the performance of your .NET Core Web API:

  1. Use Async/Await: Utilize asynchronous programming with async/await for I/O-bound operations to improve concurrency and scalability. This prevents threads from being blocked while waiting for I/O operations to complete.

  2. Response Caching: Implement response caching for frequently accessed or static data to reduce the load on the server and improve response times for subsequent requests.

  3. Use Dependency Injection (DI) Wisely: Leverage dependency injection to manage and inject dependencies efficiently. Avoid injecting unnecessary services and use scoped services appropriately to optimize resource usage.

  4. Minimize Database Round-Trips: Reduce the number of database round-trips by batching requests, optimizing queries, using caching, and implementing data access patterns like lazy loading or eager loading as per your application requirements.

  5. Optimize Entity Framework Core Queries: When using Entity Framework Core (EF Core), optimize LINQ queries by ensuring appropriate indexing, avoiding unnecessary data loading, using projection to retrieve only required fields, and optimizing data access patterns.

  6. Enable Compression: Enable response compression to reduce the size of data transferred over the network, thus improving network latency and overall performance. Use gzip or Brotli compression based on your requirements.

  7. Implement Rate Limiting: Apply rate limiting to prevent abuse or DoS attacks and to ensure fair usage of resources. Implement rate limiting policies based on IP address, user identity, or other relevant criteria.

  8. Optimize Serialization: Choose efficient serialization formats like JSON or Protobuf based on your application needs. Minimize the serialization overhead by selecting lightweight serializers and optimizing data transfer objects (DTOs) to reduce unnecessary properties.

  9. Use CDN for Static Assets: Offload static assets such as images, CSS, and JavaScript files to a Content Delivery Network (CDN) to reduce server load and improve content delivery speed by serving assets from edge servers closer to the user.

  10. Monitor and Analyze Performance: Implement logging, monitoring, and profiling to identify performance bottlenecks, track application metrics, and analyze request/response times. Utilize tools like Application Insights, Serilog, or ELK stack for comprehensive performance monitoring and analysis.

  11. Optimize Authentication and Authorization: Implement efficient authentication and authorization mechanisms to minimize overhead associated with user authentication and authorization checks. Consider using JWT authentication for stateless authentication and role-based access control (RBAC) for fine-grained authorization.

  12. Use HTTP/2: If possible, leverage HTTP/2 protocol to benefit from features like multiplexing, header compression, and server push, which can improve performance and reduce latency for HTTP requests.

By adopting these performance optimization strategies, you can enhance the responsiveness, scalability, and efficiency of your .NET Core Web API, resulting in better user experience and resource utilization. Remember to perform performance testing and profiling to validate the effectiveness of optimizations and identify further areas for improvement.

OPtimization

 Optimizing an Angular application involves various techniques to improve its performance, reduce load times, and enhance user experience. Here are several strategies to optimize your Angular application: Lazy Loading Modules: Break your application into feature modules and lazy load them. This reduces the initial bundle size and speeds up the initial load time by only loading the required modules when navigating to specific routes.

Optimize Bundle Size: Minimize the size of your application bundles by enabling production mode (ng build --prod) which performs Ahead-of-Time (AOT) compilation, tree-shaking, and minification. Additionally, consider using code-splitting techniques to further reduce bundle size.

Optimize Template Performance:

Use Angular's built-in change detection optimization techniques such as OnPush change detection strategy.

Avoid heavy computations and DOM manipulations in templates.

Use trackBy function with ngFor directive to optimize rendering performance for lists.

Optimize HTTP Requests:

Implement HTTP request caching using techniques like memoization or caching using RxJS operators like shareReplay.

Use HTTP interceptors to handle common tasks such as caching, authentication, and error handling to avoid repetition.

Implement Efficient Data Binding:

Use one-time binding ({{::data}}) for static data or data that doesn't change frequently.

Avoid excessive use of two-way data binding ([(ngModel)]) as it can impact performance negatively.

Optimize Image Loading: Compress images to reduce file size and leverage lazy loading techniques to load images only when they come into the viewport.

Enable Production Mode: Ensure that your Angular application is running in production mode (enableProdMode()) to disable development-specific features and improve runtime performance.

Optimize Routing Configuration: Minimize the number of routes and optimize route configurations by using wildcard routes or lazy loading routes for less frequently accessed features.

Remove Unused Dependencies: Regularly review and remove any unused dependencies from your Angular application to reduce bundle size and improve load times

Performance Monitoring and Profiling: Use browser developer tools, Angular Augury, or other performance monitoring tools to identify performance bottlenecks, analyze rendering times, and track memory usage.

Upgrade to the Latest Angular Version: Keep your Angular application up-to-date with the latest version to leverage performance improvements, bug fixes, and new features introduced in each release.

By implementing these optimization techniques, you can significantly improve the performance and efficiency of your Angular application, resulting in faster load times, smoother user experience, and better overall performance.

Saturday, 10 February 2024

Left Join example

   ALTER PROCEDURE security.getLeftNavbarByUserID    

    @UserID BIGINT    

AS      

BEGIN      

 -- Update the HasPermission column based on the user's access permissions

    --UPDATE LNT

    --SET HasPermission = CASE WHEN UA.CanAccess = 1 THEN 1 ELSE 0 END

    --FROM [dbo].[NavigationTabs] LNT

    --LEFT JOIN [dbo].[UserAccessEntity]  UA ON LNT.TabID = UA.TabID AND UA.UserID = @UserID;


    SELECT N.TabID, N.TabName,N.icon,N.link,N.UserID,N.Description,ISNULL(U.CanAccess,0) FROM  

[dbo].[NavigationTabs]  N    

  LEFT JOIN  [dbo].[UserAccessEntity]  U ON U.TabID=N.TabID    

  where U.UserID=@UserID    

 END 

-----------------------

Here left table i need all tabs list and alos join user access tabs only. forthis used Left Join

Friday, 9 February 2024

How to prevent calling same api in different components(rxjs shareReplay)

 

THis is the feature not calling api mutiple times.

refer Link

ts.

this.response=this.shareds.getHubList()
this.getHubList();
this.getHubList();
}
getHubList(){
  this.response.subscribe((res:any)=>{
    console.log(res)
  })

}

service

export class SharedsameApiDataService {
  m_apiUrl=environment.m_apiurl;

  constructor(private http: HttpClient) {}
  getTodosList() {
    return this.http.get(this.m_apiUrl+'products/getLookup').pipe(shareReplay());
  }
  getHubList() {
    return this.http.get('http://localhost:8080/api/hubs/get').pipe(shareReplay());
  }

1.Here hub list api need in mutiple componet but calling api using shareReplay







Monday, 5 February 2024

c#

 https://github.com/dcyuksel/Result